简体   繁体   English

对javascript局部变量VS全局变量感到困惑

[英]confused about javascript local variable VS global variable

Here is the code below: 这是下面的代码:

 var num1 = document.getElementById('num1').value; var num2 = document.getElementById('num2').value; function Multiplication (num1,num2) { document.getElementById('result').innerHTML = num1*num2; } function Divide (num1,num2) { document.getElementById('result').innerHTML = num1/num2; } 
 <form> <input type="number" value="2" id="num1"/> <input type="number" value="3" id="num2" /> <input type="button" value="Multiplication" onClick="Multiplication()"/> <input type="button" value="Divide" onClick="Divide()"/> </form> <div> <span>The result is:</span> <p id="result"></p> </div> 

I am getting result NAN when i click the multiplication or divide button to trigger the function,but i thought num1 and num2 both are global variable,could someone help me figure out why the both undefined? 当我单击乘法或除法按钮以触发该函数时,我得到的结果是NAN,但我认为num1和num2都是全局变量,有人可以帮我弄清楚为什么这两个未定义吗?

The way you set up your functions, 设置功能的方式,

function Multiplication (num1,num2){...}

this functions is expecting two values to be given when the function is called, currently the button that calls the function doesn't pass any variables. 此函数期望在调用函数时给出两个值,当前调用该函数的按钮未传递任何变量。

onClick="Multiplication()

To remedy this you would either have to remove the values when the function is defined like this, 为了解决这个问题,您必须在定义函数时删除这些值,

function Multiplication (){...}

Thus instead of looking for the variables from the button that called it, it would then look for them inside the script. 因此,与其在调用按钮的按钮中查找变量,不如在脚本中查找变量。 If that doesn't work then change your javascript to something like this, 如果这样不起作用,请将您的JavaScript更改为类似的内容,

var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;


function Multiplication () {
   var num1 = document.getElementById('num1').value;
   var num2 = document.getElementById('num2').value;
   document.getElementById('result').innerHTML =  num1*num2;

}

function Divide () {
   var num1 = document.getElementById('num1').value;
   var num2 = document.getElementById('num2').value;
   document.getElementById('result').innerHTML =  num1/num2;
} 

This should work, if you have any other questions don't hesitate to ask! 如果您有任何其他问题,请不要犹豫,这应该可以!

In your onclick event there is no arguments passed to these two functions. 在您的onclick事件中,没有参数传递给这两个函数。

Remove arguments and try getting num1 and num2 from within the functions you declared. 删除参数并尝试从声明的函数中获取num1和num2。

The are two major problems (and one minor) with your code: 您的代码有两个主要问题(一个是次要问题):

First of all, your functions Multiplication and Divide have two arguments num1 and num2 that shadow the global ones, but you're calling the functions with no arguments, so inside the functions, these will have the value undefined . 首先,您的函数MultiplicationDivide有两个参数num1num2 ,它们遮蔽了全局参数,但是您调用的函数没有参数,因此在函数内部,它们的值将为undefined Multiplying or dividing undefined by itself gives NaN ( not a number ). 本身通过undefined的乘法或除法得到NaN( 不是数字 )。

Secondly, even if you remove the arguments from your functions, num1 and num2 are the values of the two fields when the code is running , that is, when the page is loading. 其次,即使从函数中删除了参数, num1num2也是代码运行时(即页面加载时)两个字段的值。 So if you change the value in the input fields, pressing the buttons would not give a different result. 因此,如果您在输入字段中更改值,则按按钮不会产生不同的结果。

Third, your variables num1 and num2 are strings , not numbers, since .value of an input field gives a string. 第三,变量num1num2字符串 ,而不是数字,因为输入字段的.value给出了字符串。 This won't make a difference when you're multiplying or dividing strings, but if you're adding two strings, then '1' + '1' == '11' instead of 2, so you should use Number() to convert your strings into numbers. 当您乘以或除以字符串时,这没有什么区别,但是如果要添加两个字符串,则使用'1' + '1' == '11'而不是2,因此应使用Number()将您的字符串转换为数字。

Taking these things into account, you'd end up with something like this: 考虑到这些因素,您最终会得到如下结果:

 function Multiplication () { var num1 = Number(document.getElementById('num1').value); var num2 = Number(document.getElementById('num2').value); document.getElementById('result').innerHTML = num1*num2; } function Divide () { var num1 = Number(document.getElementById('num1').value); var num2 = Number(document.getElementById('num2').value); document.getElementById('result').innerHTML = num1/num2; } 
 <form> <input type="number" value="2" id="num1"/> <input type="number" value="3" id="num2" /> <input type="button" value="Multiplication" onClick="Multiplication()"/> <input type="button" value="Divide" onClick="Divide()"/> </form> <div> <span>The result is:</span> <p id="result"></p> </div> 

Global and local variables concept is same almost in all languages. 全局变量和局部变量的概念几乎在所有语言中都是相同的。

//global variables
var num1=parseInt(document.getElementById('num1').value);
var num2=parseInt(document.getElementById('num2').value);

function Multiplication () {
      document.getElementById('result').innerHTML =  num1*num2;
}

function Divide () {
    document.getElementById('result').innerHTML =  num1/num2;
}

//local variables
function Multiplication () {
    var num1=parseInt(document.getElementById('num1').value);
    var num2=parseInt(document.getElementById('num2').value);
    document.getElementById('result').innerHTML= num1*num2;
}

function Divide () {
    var num1=parseInt(document.getElementById('num1').value);
    var num2=parseInt(document.getElementById('num2').value);
     document.getElementById('result').innerHTML=num1/num2;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM