在下面的代码中,是否可以从嵌套对象字面量访问 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.
}
最佳答案
按照您的说法 - 不。
你需要两个阶段的 build ,这会起作用:
var outer = { x : 0 };
// outer is constructed at this point.
outer.inner = {
b : outer.x + 1 // 'outer' is defined here.
};
关于javascript - 如何从嵌套对象文字访问外部成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5286705/