简体   繁体   English

如何在不使用全局变量的情况下将局部变量链接到其他函数?

[英]How do you link local variables to other functions without having to use global variables?

How do I link up the 3 functions below so I can have mathFormula output an answer?如何链接下面的 3 个函数,以便获得mathFormula output 的答案?

HTML text: HTML 文本:

<div id = "rowContainer"></div>

JavaScript text: JavaScript 文字:

function mathFormula(x, y) {
    return x * y;
}

const mainDiv = document.getElementById("rowContainer"); 

function newRow() {
    const input1 = document.createElement("input");
    mainDiv.appendChild(input1);
    const input2 = document.createElement("input");
    mainDiv.appendChild(input2);
    const result = document.createElement("div");
    mainDiv.appendChild(result);
}

mainDiv('keyup', function() {
    const a = input1.value; 
    const b = input2.value; 
    const sum = mathFormula(a,b); 
    result.textContent = sum; 
})

Functions can see variables in their containing scope, so the keyup listener, if defined inside the newRow function , captures the context of input elements.函数可以在包含 scope 的变量中看到变量,因此如果在 newRow function 中定义了 keyup 侦听器,它将捕获输入元素的上下文。

The other thing to notice is that the listener is ambiguous unless attached to a specific row (capturing in scope that row's inputs).要注意的另一件事是,除非附加到特定行(在 scope 中捕获该行的输入),否则侦听器是模棱两可的。 The snippet below adds an extra div to hold the row, and attaches the listener at the row-div level.下面的代码片段添加了一个额外的 div 来保存行,并在 row-div 级别附加侦听器。

 const mainDiv = document.getElementById('maindiv'); function mathFormula(x, y) { return x * y; } function addRow() { const rowDiv = document.createElement("div"); mainDiv.appendChild(rowDiv); const input1 = document.createElement("input"); rowDiv.appendChild(input1); const input2 = document.createElement("input"); rowDiv.appendChild(input2); const result = document.createElement("div"); rowDiv.appendChild(result); rowDiv.addEventListener("keyup", event => { const a = input1.value; const b = input2.value; result.textContent = mathFormula(a, b); }) } addRow() addRow()
 <div id="maindiv"> </div>

We only need 2 functions in this case.在这种情况下,我们只需要 2 个函数。 newRow() & mathFormula . newRow()mathFormula creating 3 inputs instead of one being a div.创建 3 个inputs而不是一个 div。 appending all inputs inside our rowContainer .all at once, no need to do them separately.一次将所有输入附加到我们的rowContainer中,无需单独执行。 then i am listening to a click on my button which i created inside html.然后我正在点击我在 html 中创建的按钮。 so,when we click the button.inside there i am calling our mathFormula function & passing the input1 & input2.所以,当我们点击按钮时。在里面我调用我们的mathFormula并传递输入 1 和输入 2。

let multiply=mathFormula(input1,input2) which sends the these two inputs to mathFormula as object still.those inputs are then assigned to x,y. let multiply=mathFormula(input1,input2)将这两个输入作为 object Still 发送给 mathFormula。然后将这些输入分配给 x,y。 then i am saying x = x.value & y =y.value then returns a single value of x*y which gets stored in the multiply variable.然后我说x = x.value & y =y.value然后返回x*y的单个值,该值存储在multiply变量中。 because we said.因为我们说过。

let multiply=mathFormula(input1,input2) putting that value inside result input. let multiply=mathFormula(input1,input2)将该值放入结果输入中。

i hope it helps you.you can ask if you have any doubts我希望它对你有帮助。如果你有任何疑问,你可以问

 const mainDiv = document.getElementById("rowContainer"); const btn = document.getElementById("btn"); function mathFormula(x, y) { x=x.value; y=y.value return x * y; } newRow(); /*-----Calling the newRow() rightAway*/ function newRow() { const input1 = document.createElement("input"); const input2 = document.createElement("input"); const result = document.createElement("input"); mainDiv.append(input1, input2, result); btn.addEventListener("click",()=>{ let multiply=mathFormula(input1,input2) result.value=multiply; }) }
 <div id="rowContainer"></div> <button id="btn">Click</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用全局变量的情况下从其他函数访问变量 - Accessing variables from other functions without using global variables 您如何从从 index.html 导入的 Javascript 脚本中访问全局变量和函数并在组件中使用它们? - How do you gain access to global variables and functions from Javascript scripts imported from index.html and use them in a component? Cloud Functions - 使用全局变量 - Cloud Functions - Use global variables 如何将webgl函数作为变量传递 - How do you pass webgl functions as variables 如何将Google Maps与Marko.js和Lasso.js结合使用以及如何将全局变量链接到其他模板 - How to use Google Maps with Marko.js and Lasso.js and link global variables to other templates 如何在不使用全局变量的情况下使数据随时可用于多个Javascript函数? - How can I make data readily available to multiple Javascript functions without use of global variables? 您如何要求没有前缀的变量 - How do you require variables without a prefix 如何在JavaScript中设置全局变量和全局函数 - How to set global variables and global functions in JavaScript 如何在Javascript的预定义全局变量中使用原型函数? - How to use the prototype functions in predefined global variables in Javascript? 在Node.js中的其他Promise函数中访问Firebase Fireproof Promise Promise局部变量-避免全局 - Access firebase fireproof promise local variables in other promise functions in Node.js- avoid global
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM