是否有代表本地范围的Javascript变量?像全球?

时间:2022-03-23 16:50:01

global is an object containing any global variables (at least in Node.js, they're in window in the browser).

global是一个包含任何全局变量的对象(至少在Node.js中,它们位于浏览器的窗口中)。

Is there a similar variable that represents the current scope? Local variables don't show up in global (for a good reason :) )

是否有代表当前范围的类似变量?局部变量不会出现在全局中(有充分理由:))

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

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

1 个解决方案

#1


21  

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. Within the execution Context, an Activation object(also known as call object) is used to create local variables as its property. However,

为什么? JavaScript只有函数作用域 - 即执行Context。在执行Context中,Activation对象(也称为调用对象)用于创建局部变量作为其属性。然而,

...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

参考

#1


21  

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. Within the execution Context, an Activation object(also known as call object) is used to create local variables as its property. However,

为什么? JavaScript只有函数作用域 - 即执行Context。在执行Context中,Activation对象(也称为调用对象)用于创建局部变量作为其属性。然而,

...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

参考