Javascript 中的 window 真的是全局的吗? [英] Is window really global in Javascript?

查看:18
本文介绍了Javascript 中的 window 真的是全局的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器中获取这段 Javascript:

Take this piece of Javascript in a browser:

<script>

console.log(window.someThing);
var x = 12;

function foo() {
   window.otherThing = x;
}

</script>

foo里面我们可以访问window,我们都知道,但究竟是为什么呢?

Inside foo we can access window, we all know that, but why exactly?

  • 它是某种特殊的全局变量吗?
  • 或者根范围"(在 script 标签内)是否将其作为隐式局部变量,它是否只是闭包继承"作为任何其他局部变量(如 x 以上)可以吗?
  • Is it some kind of special global variable?
  • Or does the "root scope" (inside the script tag) have it as an implicit local variable and is it simply "closure-inherited" as any other local variable (like x above) can be?

这与直接在 script 标签内声明的变量被设置为 window 的属性有何一致?(或者不是这样?)

And how does that concur with variables declared directly inside the script tag being set as properties of window? (Or is that not so?)

<script>
var x = 12;
function() {
   console.log(window.x);
}
</script>

推荐答案

您可以在 ECMAscript 中访问 超出范围"免费" 变量的原因是所谓的作用域链.作用域链是来自每个执行上下文的特殊属性.如前所述,上下文对象至少看起来像:

The reason why you can access "out of scope" or "free" variables in ECMAscript is the such called Scope chain. The scope chain is a special property from each Execution context. As mentioned several times before, a context object looks at least like:

  • [[范围]]
  • 变量/激活对象
  • 这个"上下文值

每次在上下文(例如函数)中访问变量(-name)时,查找过程总是从它自己的Activation Object 开始.所有形式参数、函数声明和本地定义的变量 (var) 都存储在该特殊对象中.如果在该对象中未找到变量名,则搜索进入 [[Scope]] 链.每次初始化函数(-context) 时,它都会将所有父上下文变量/激活对象 复制到其内部[[Scope]] 属性中.这就是我们所说的,词法范围.这就是 Closures 在 ECMAscript 中工作的原因.由于全局上下文还有一个变量对象(更准确地说,**全局对象的变量对象是全局对象本身)它也被复制到函数中[[Scope]] 属性.

each time you access a variable(-name) within a context (a function for instance), the lookup process always starts in it's own Activation Object. All formal parameters, function declarations and locally defined variables (var) are stored in that special object. If the variablename was not found in that object, the search goes into the [[Scope]]-chain. Each time a function(-context) is initialized, it'll copy all parent context variable/activation objects into its internal [[Scope]] property. That is what we call, a lexical scope. That is the reason why Closures work in ECMAscript. Since the Global context also has an Variable Object (more precisely, **the variable object for the global object is the global object itself) it also gets copied into the functions [[Scope]] property.

这就是为什么您可以从任何函数中访问 window 的原因:-)

That is the reason why you can access window from within any function :-)

上述解释有一个重要的概念性结论:ECMAscript 中的任何函数都是一个闭包,这是正确的.因为每个函数至少会复制[[Scope]]属性中的全局上下文VO.

The above explanation has one important conceptional conclusion: Any function in ECMAscript is a Closure, which is true. Since every function will at least copy the global context VO in its [[Scope]] property.

这篇关于Javascript 中的 window 真的是全局的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆