是否有表示本地范围的Javascript变量?像全球? [英] Is there a Javascript variable that represents local scope? Like global?
问题描述
global
是一个包含任何全局变量的对象(至少在Node.js中,它们位于窗口
在浏览器中)。
global
is an object containing any global variables (at least in Node.js, they're in window
in the browser).
是否有代表当前范围的类似变量?局部变量不会显示在全局
中(有充分理由:))
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?
推荐答案
是否有对象代表本地范围?
是的。有。
Is there an object represents the local scope?
Yes. There is.
没有。你不能。
为什么?
JavaScript只有函数作用域 - 这是执行上下文。在执行上下文中,激活对象(也称为调用对象
)用于创建局部变量属性。但是,
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代码直接引用。
...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变量?像全球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!