简体   繁体   English

Javascript在全局范围速记中声明变量

[英]Javascript declare variable in global scope shorthand

var object = {name: "Murad"};
(function(window){
  var a = b = 10;
})(object)

Why I can access b variable in global scope? 为什么我可以在全局范围内访问b变量?

var a = b = 10

is not same thing with? 不一样吗?

window.b = 10;
var a = window.b;

You can access variable b because you give a variable name - b, but you do not declare it like var let or const. 您可以访问变量b,因为您提供了变量名-b,但是您没有像var let或const那样声明它。 JS will see that a name is allocated for a variable and it will create the variable automatically in the global scope. JS将看到为变量分配了一个名称,它将在全局范围内自动创建该变量。

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

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