简体   繁体   English

由于全局范围变量,导致JavaScript错误

[英]javascript error because of global scope variables

var x = 3;
(function (){
  console.log('before', x);
  var x = 7;
  console.log('after', x);
  return ;
})();

In the above code var X is initialized globally. 在上面的代码中,var X是全局初始化的。 So inside the function the first console.log should print "before 3" but i don't get it. 因此,在函数内部,第一个console.log应该打印“ before 3”,但我不明白。 The reason is that i am trying to re-declare the global variable. 原因是我试图重新声明全局变量。

Can somebody explain why this is happening? 有人可以解释为什么会这样吗?

In the above code var X is initialized globally. 在上面的代码中,var X是全局初始化的。 so inside the function the first console.log should print "before 3". 因此在函数中,第一个console.log应该打印“ before 3”。

No, it should print before undefined , because var takes effect from the beginning of the function regardless of where you write it. 不,它应该before undefined打印,因为var从函数的开头开始生效,无论您在哪里编写它。

Your code is exactly the same as this: 您的代码与此完全相同

var x = 3;
(function (){
 var x;

 console.log('before', x);
 x = 7;
 console.log('after', x);
 return ;
})();

And of course, variables start with the value undefined . 当然,变量以undefined值开头。

Details: Poor misunderstood var 详细信息: var误解少

The JavaScript parser does Variable Hoisting when parsing your code. JavaScript解析器在解析代码时执行变量提升 This means that any variable declaration will be moved to the top of the current scope, thus in your case, this code will get executed: 这意味着任何变量声明都将移至当前作用域的顶部,因此,在您的情况下,将执行以下代码:

var x = 3;
(function (){
  var x;
  console.log('before', x);
  x = 7;
  console.log('after', x);
  return ;
})();

So your local variable x gets declared at first with an initial value of undefined . 因此,您的局部变量x首先会以undefined的初始值声明。

This should explain, why you get an "beforeundefined" for the first console.log() . 这应该可以解释为什么第一个console.log()会得到“ beforeundefined”。

The scope of a variable is much simpler than in other languages. 变量的范围比其他语言要简单得多。 It doesn't start at declaration but is either : 它不是从声明开始,而是:

  • the function in which you have the declaration 声明所在的函数
  • the global scope if the declaration isn't in a function 如果声明不在函数中,则为全局范围

MDN : MDN

The scope of a variable declared with var is the enclosing function or, for variables declared outside a function, the global scope (which is bound to the global object). 用var声明的变量的作用域是封闭函数,对于在函数外部声明的变量,作用域是全局作用域(绑定到全局对象)。

You can imagine that all variable declarations are moved to the start of the scope (the function). 您可以想象所有变量声明都移动到作用域(函数)的开头。 So this is exactly like 所以这就像

var x = 3;
(function (){
  var x;
  console.log('before', x); // now undefined
  x = 7;
  console.log('after', x); // now 7
  return ;
})();

Be careful to understand what is the exact scope (the function, not the block) : 注意了解确切的范围是什么(函数,而不是块):

var x = 3;
(function (){
  console.log('before', x); // this is undefined !
  if (true) {
      var x = 7;
  }
  return ;
})();

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

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