如何从嵌套对象文字访问外部成员?

Nix*_*xuz 7 javascript scope nested object-literal

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

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.
}
Run Code Online (Sandbox Code Playgroud)

c-s*_*ile 9

在你说的方式 - 没有.

你需要两个阶段的构造,这将工作:

var outer = { x : 0 };
// outer is constructed at this point.
outer.inner = {
        b : outer.x + 1 // 'outer' is defined here.
};
Run Code Online (Sandbox Code Playgroud)