简体   繁体   English

javascript全局变量-函数范围?

[英]javascript global variables - scope for functions?

I tried making a simple program using javascript which will calculate the sum and average of two numbers entered in the textfields. 我尝试使用javascript制作一个简单的程序,该程序将计算在文本字段中输入的两个数字的总和和平均值。

<script>
var a=parseInt(document.getElementById("text1").value);
var b=parseInt(document.getElementById("text2").value);
function sum()
{
var result = document.getElementById("valueofsum");
var sum=a+b;
result.innerHTML = sum;
}

function average()
{
var result = document.getElementById("valueofavg");
var avg=(a+b)/2;
result.innerHTML = avg;

}

function reset()
{
 document.getElementById("text1").value = "";
 document.getElementById("text2").value = "";
 document.getElementById("valueofsum").innerHTML = "";
 document.getElementById("valueofavg").innerHTML = "";
}
</script>

The two variables which reads the values from textfields are globally declared and initialized as "a" and "b". 从文本字段读取值的两个变量被全局声明并初始化为“ a”和“ b”。 The sum and average are returned as NaN(NotaNumber) instead of the calculated results. 总和和平均值作为NaN(NotaNumber)而不是计算结果返回。

If i use/declare both the variables inside the functions as local variables the code seems out to be working properly which makes me wonder about the scope of global variables in javascript? 如果我将函数内部的两个变量都声明为局部变量,则代码似乎正常工作,这使我想知道javascript中全局变量的范围吗?

You are reading the values from the inputs when the page loads (at which point they are, presumably, empty strings). 当页面加载时,您正在从输入中读取值(此时它们大概是空字符串)。

If you want to get the values at the time the sum or average functions run (ie after the user has entered something into them), then you need to fetch their values when those functions run . 如果要在sumaverage函数运行时(即在用户向其中输入内容之后)获取值,则需要在这些函数运行时获取其值。

See the following code example: 请参见以下代码示例:

function sum() {
  var a=parseInt(document.getElementById("text1").value);
  var b=parseInt(document.getElementById("text2").value);
  var result = document.getElementById("valueofsum");
  var sum=a+b;
  result.innerHTML = sum;
}

You are not providing any HTML code , so i guess you have placed your script at the start of your page and the HTML code follows after it resulting in your code running prior to the DOM creation , thus no "text1" and "text2" elements actually exist at that point. 您没有提供任何HTML代码,因此我想您已将脚本放置在页面的开头,并且HTML代码紧随其后,导致代码在DOM创建之前运行,因此没有“ text1”和“ text2”元素当时确实存在。

When you place them inside the Function , you simply avoid the above issue , cause i suspect you are executing these functions at the end of your Page , or with any onClick event somewhere. 将它们放在Function中时,您只需避免上述问题,原因是我怀疑您是在Page的末尾或某些地方的onClick事件执行这些函数。

If that is not the case , please provide a bit more insight of the structure of your page. 如果不是这种情况,请提供更多有关页面结构的见解。

You are getting values outside, in global scope and when those lines were executed, values were empty. 您正在全局范围之外获取值,并且在执行这些行时,值是空的。 You need to get the fields reference in global scope to avoid repetition but get values in side functions. 您需要在全局范围内获取字段引用,以避免重复,但要在侧函数中获取值。

<script>
  var a = document.getElementById("text1");
  var b = document.getElementById("text2");
  function sum()
  {
  var result = document.getElementById("valueofsum");
  var sum = parseInt(a.value) + parseInt(b.value);
  result.innerHTML = sum;
  return sum;
  }

  function average()
  {
  var result = document.getElementById("valueofavg");
  var avg= sum()/2;
  result.innerHTML = avg;
  return avg;
  }

  function reset()
  {
   document.getElementById("text1").value = "";
   document.getElementById("text2").value = "";
   document.getElementById("valueofsum").innerHTML = "";
   document.getElementById("valueofavg").innerHTML = "";
  }
</script>

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

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