在 JavaScript 中从执行上下文中检索值 [英] Retrieving values from Execution Contexts in JavaScript

查看:40
本文介绍了在 JavaScript 中从执行上下文中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var value = 10;

var outer_funct = function(){
    var value = 20;

    var inner_funct = function(){
        var value = 30;

        console.log(value); // logs 30
        console.log(window["outer_funct"]["value"]); // What I would like to log here is the value 20.
        console.log(window["value"]); // logs 10
    };

    inner_funct();
};

outer_funct();

我相信第二个日志返回 undefined 的原因是 window["outer_funct"] 指的是函数对象,而函数对象没有与之关联的属性值".相反,我想做的是在调用 window["outer_funct"] 时引用 执行上下文.这可以在 inner_funct 的执行上下文中完成吗?

I believe the reason the second log is returning undefined is because window["outer_funct"] refers to the function object, and the function object doesn't have a property "value" associated with it. Instead, what I would like to do is refer to the execution context when window["outer_funct"] is invoked. Is this possible to do within the execution context of inner_funct?

推荐答案

我认为第二个日志返回 undefined 的原因是 window["outer_funct"] 引用了函数对象,而函数对象没有与之关联的属性值".

I believe the reason the second log is returning undefined is because window["outer_funct"] refers to the function object, and the function object doesn't have a property "value" associated with it.

正确.

相反,我想做的是在调用 window["outer_funct"] 时引用执行上下文.这可以在 inner_funct 的执行上下文中完成吗?

Instead, what I would like to do is refer to the execution context when window["outer_funct"] is invoked. Is this possible to do within the execution context of inner_funct?

不,不是因为你隐藏了 value(在 inner_funct 中声明).如果那个符号被这样覆盖,你就无法获得它.当然,您可以将其抓取到另一个符号中:

No, not with you having shadowed value (declared it in inner_funct). You have no way of getting to it with that symbol having been overridden like that. You could, of course, grab it into another symbol:

var value = 10;

var outer_funct = function(){
    var value = 20;

    var outer_value = value;

    var inner_funct = function(){
        var value = 30;

        console.log(value);        // logs 30
        console.log(outer_value);  // logs 20
        console.log(window.value); // logs 10
    };

    inner_funct();
};

outer_funct();

如果你没有隐藏它,那么你可以在包含上下文中引用value,例如:

If you hadn't shadowed it, then you could refer to value in the containing context, e.g.:

var value1 = 10;

var outer_funct = function(){
    var value2 = 20;

    var inner_funct = function(){
        var value3 = 30;

        console.log(value3); // logs 30
        console.log(value2); // logs 20
        console.log(value1); // logs 10
    };

    inner_funct();
};

outer_funct();

值得注意的是,您的原始代码的 window["value"] 返回 10 的唯一原因(顺便说一句,您也可以使用 window.value) 是 var value = 10; 在全局范围内.所有用 var 声明的变量都成为全局对象的属性,在浏览器上通过 window 引用(从技术上讲,window 本身就是指向全局对象的全局对象上的一个属性).

It's worth noting that the only reason that your original code's window["value"] returned 10 (btw, you could also use window.value) is that the var value = 10; is at global scope. All variables declared with var become properties of the global object, which on browsers is referred to via window (technically, window is, itself, just a property on the global object that points back to the global object).

这篇关于在 JavaScript 中从执行上下文中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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