简体   繁体   English

是否有一个Javascript变量代表本地范围? 喜欢全球?

[英]Is there a Javascript variable that represents local scope? Like global?

global is an object containing any global variables (at least in Node.js, they're in window in the browser). global是一个包含任何全局变量的对象(至少在Node.js中,它们在浏览器的window中)。

Is there a similar variable that represents the current scope? 是否有一个类似的变量代表当前范围? Local variables don't show up in global (for a good reason :) ) 局部变量不会显示在global变量中(有充分的理由:))

asdf = "hello";
var local = "hello";

console.log(global); // includes asdf
console.log(???);    // includes local?

Is there an object represents the local scope? 是否有一个代表本地范围的对象?

Yes. 是。 There is. 有。

Could you access the object (directly)? 您可以直接访问该对象吗?

No. You can't. 不,你不能。

Why? 为什么? JavaScript has only function scope - which is the execution Context . JavaScript仅具有函数作用域-这是执行上下文 Within the execution Context , an Activation object (also known as call object ) is used to create local variables as its property. 执行上下文中 ,一个Activation对象 (也称为call object )用于创建局部变量作为其属性。 However, 然而,

...it is not a normal object as it has no prototype (at least not a defined prototype) and it cannot be directly referenced by javascript code. ...这不是普通对象,因为它没有原型(至少不是已定义的原型),并且无法由javascript代码直接引用。

Reference 参考

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

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