如何从嵌套对象文字访问外部成员? [英] How to access an outer member from a nested object literal?

查看:68
本文介绍了如何从嵌套对象文字访问外部成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,是否可以从嵌套对象文字中访问x成员?

In the following code, can the x member be accessed from the nested object literal?

var outer = {
    x : 0,
    inner: {
        a : x + 1,       // 'x' is undefined.
        b : outer.x + 1, // 'outer' is undefined.
        c : this.x + 1   // This doesn't produce an error, 
    }                    // but outer.inner.c is NaN.
}


推荐答案

顺便提一下 - 不。

In the way you put it - no.

你需要两个阶段构建,这将有效:

You need two stages construction, this will work:

var outer = { x : 0 };
// outer is constructed at this point.
outer.inner = {
        b : outer.x + 1 // 'outer' is defined here.
};

这篇关于如何从嵌套对象文字访问外部成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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