简体   繁体   English

访问函数内定义的全局变量

[英]Accessing global variable defined inside a function

I am trying to understand variable scopes in Javascript and a friend asked me this question.我试图理解 Javascript 中的变量范围,一个朋友问了我这个问题。 I could use some help here.我可以在这里使用一些帮助。

function abc(){
  var a = b = 10; // a is local, b is global variable. Right?
  c = 5; // c is global variable
}
d = 20; // d is global variable

console.log(b); // b is not defined error. Why? Isn't b a global variable?
console.log(c); // Again, Why is 'c not defined'.

I ran this code in chrome console.我在 chrome 控制台中运行了这段代码。 Shouldn't I expect 10 and 5 in console?我不应该在控制台中期待 10 和 5 吗? It gives a 'b is not defined', 'c is not defined' error instead.它给出了“b 未定义”,“c 未定义”错误。 Why does this happen if b, c are global variables?如果 b, c 是全局变量,为什么会发生这种情况? console.log(d) works just fine. console.log(d) 工作得很好。

Here's a fiddle .这是一个小提琴

Why does this happen if b , c are global variables?如果b , c是全局变量,为什么会发生这种情况?

b and c are only created if you actually call abc . bc仅在您实际调用abc创建。 Merely defining the function does not magically execute its body.仅仅定义函数并不能神奇地执行它的主体。

function abc(){
  var a = b = 10; // a is local, b is global variable.
  c = 5; // c is global variable
}

// call abc to execute the statements inside the function
abc();

console.log(b); // 10
console.log(c); // 5

That said, implicitly creating global variables is not good still.也就是说,隐式创建全局变量仍然不好。 Avoid globals if possible, and if not, explicitly declare them by assigning to the global object ( window in browsers).如果可能,请避免使用全局变量,如果没有,请通过分配给全局对象(浏览器中的window )来显式声明它们。

EDIT: This is why I love SO, you learn something knew even when you're a know it all and answer questions you clearly are not equipped to answer.编辑:这就是我喜欢 SO 的原因,即使您无所不知,您也能学到一些知识,并回答您显然无法回答的问题。 Thanks to @FelixKling's clarification I have updated this to reflect that var s感谢@FelixKling 的澄清,我已经更新了这个以反映var s

So there's a little bit of a terminology confusion going on here:所以这里有一些术语混淆:

Javascript has function level scope : as a and b are declared inside the function block they are local to the function they were declared within and are constrained to the function 's scope. Javascript 具有函数级作用域:由于abfunction块内声明,它们对于它们在其中声明的function是本地的,并且被限制在function的作用域内。

Variables defined outside a function (or in a browser on the window object, in node on the global object), have global scope.函数外部(或在window对象的浏览器中,在global对象的 node 中)定义的变量具有global作用域。

So the var keyword doesn't actually have anything to do with global scope, scope is defined by where you declare a variable.所以var关键字实际上与global作用域没有任何关系,作用域由您声明变量的位置定义。

Taking your example:以你的例子为例:

function abc(){
  var a = b = 10; //a is local, b is global (see @FelixKling's answer)
  c = 5; // c is global as it is not prefixed with the `var` keyword 
}
var d = 20; // d is global 


console.log(a); // logs "10" to the console
console.log(b); // b is not defined
console.log(c); // 'c not defined'.
console.log(d); // logs 20 to the console. 

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

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