是什么窗口真的全球化在Javascript? [英] Is window really global in Javascript?

查看:84
本文介绍了是什么窗口真的全球化在Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器中使用这部分Javascript:

 < script& 

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

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

< / script>

内部 foo $ c> window ,我们都知道,但是为什么呢?




  • 变量?

  • 或者根范围(位于脚本标记中)将其作为隐式局部变量, 封闭继承为任何其他局部变量(如上面的 x )可以是?


$ b b

它如何与直接在脚本标签中声明的窗口标签的变量一致? (或者不是这样?)

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


解决方案

您可以访问在ECMAscript中的范围免费变量是这样的调用 Scope chain 。范围链是每个执行上下文的特殊属性。如前所述,上下文对象看起来至少像:




  • [[scope]]

  • 变量/激活对象

  • 此上下文值



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



这是为什么你可以访问<上面的解释有一个重要的概念性结论:ECMAscript中的任何函数都是一个 c> c $ c> window em>关闭,这是真的。因为每个函数至少会在其 [[Scope]] 属性中复制全局上下文VO


Take this piece of Javascript in a browser:

<script>

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

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

</script>

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

  • 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?

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>

解决方案

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:

  • [[scope]]
  • Variable / Activation Object
  • "this" context value

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.

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

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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