属性 的 属性 没有 'this' 作为对根对象的引用
Property of a property doesn't have 'this' as a reference to the root object
我创建了以下变量:
var orientations = {
E: {R: this.S, L: this.N},
W: {R: this.N, L: this.S},
N: {R: this.E, L: this.W},
S: {R: this.W, L: this.E},
}
我正在尝试引用我的 orientations 对象,但在我的代码中 'this' 引用了 window 对象。我猜这可能是因为我对对象有两层深度。
有什么方法可以引用 orientations 对象本身吗?
您想要使用的方式 this
无效。如果 this
在新的词法范围内,它只会以您想要的方式定义。看到这个问题Self-references in object literals / initializers
尽我所能...(还有更好的吗?)
const orientations= { E: { RL: 'SN' }
, W: { RL: 'NS' }
, N: { RL: 'EW' }
, S: { RL: 'WE' }
}
for(let o in orientations) {
orientations[o].R = orientations[ orientations[o].RL.charAt(0) ]
orientations[o].L = orientations[ orientations[o].RL.charAt(1) ]
delete orientations[o].RL // no more needed
}
console.log( orientations.W.R === orientations.N )
您的代码能够访问 this
:
的一种方法
var obj = {
E: {},
W: {},
N: {},
S: {},
initMyself: function() {
Object.assign(this.E, {R: this.S, L: this.N});
Object.assign(this.W, {R: this.N, L: this.S});
Object.assign(this.N, {R: this.E, L: this.W});
Object.assign(this.S, {R: this.W, L: this.E});
}
};
obj.initMyself();
console.log(obj.E.R === obj.S);
简单的规则是,当您有 obj.fn()
时,然后在 fn()
的代码中,this
绑定到 obj
。但是如果你有 g = obj.fn;
然后 g()
,g()
中的代码将 this
绑定到全局对象。
并且您的要求也可能有 "self" 参考。您正在设置 E.R
以引用 S
,但 S
尚不存在。所以这就是为什么我的代码首先创建它们,然后 Object.assign()
几乎只是复制属性。
Object.assign(this.E, {R: this.S, L: this.N});
只是
this.E.R = this.S;
this.E.L = this.N;
您的代码可能的一种方式是:
var orientations = (function() {
var obj = {
E: {},
W: {},
N: {},
S: {},
initMyself: function() {
Object.assign(this.E, {R: this.S, L: this.N});
Object.assign(this.W, {R: this.N, L: this.S});
Object.assign(this.N, {R: this.E, L: this.W});
Object.assign(this.S, {R: this.W, L: this.E});
}
};
obj.initMyself();
delete obj.initMyself;
return obj;
}());
console.log(orientations.E.R === orientations.S);
我创建了以下变量:
var orientations = {
E: {R: this.S, L: this.N},
W: {R: this.N, L: this.S},
N: {R: this.E, L: this.W},
S: {R: this.W, L: this.E},
}
我正在尝试引用我的 orientations 对象,但在我的代码中 'this' 引用了 window 对象。我猜这可能是因为我对对象有两层深度。 有什么方法可以引用 orientations 对象本身吗?
您想要使用的方式 this
无效。如果 this
在新的词法范围内,它只会以您想要的方式定义。看到这个问题Self-references in object literals / initializers
尽我所能...(还有更好的吗?)
const orientations= { E: { RL: 'SN' }
, W: { RL: 'NS' }
, N: { RL: 'EW' }
, S: { RL: 'WE' }
}
for(let o in orientations) {
orientations[o].R = orientations[ orientations[o].RL.charAt(0) ]
orientations[o].L = orientations[ orientations[o].RL.charAt(1) ]
delete orientations[o].RL // no more needed
}
console.log( orientations.W.R === orientations.N )
您的代码能够访问 this
:
var obj = {
E: {},
W: {},
N: {},
S: {},
initMyself: function() {
Object.assign(this.E, {R: this.S, L: this.N});
Object.assign(this.W, {R: this.N, L: this.S});
Object.assign(this.N, {R: this.E, L: this.W});
Object.assign(this.S, {R: this.W, L: this.E});
}
};
obj.initMyself();
console.log(obj.E.R === obj.S);
简单的规则是,当您有 obj.fn()
时,然后在 fn()
的代码中,this
绑定到 obj
。但是如果你有 g = obj.fn;
然后 g()
,g()
中的代码将 this
绑定到全局对象。
并且您的要求也可能有 "self" 参考。您正在设置 E.R
以引用 S
,但 S
尚不存在。所以这就是为什么我的代码首先创建它们,然后 Object.assign()
几乎只是复制属性。
Object.assign(this.E, {R: this.S, L: this.N});
只是
this.E.R = this.S;
this.E.L = this.N;
您的代码可能的一种方式是:
var orientations = (function() {
var obj = {
E: {},
W: {},
N: {},
S: {},
initMyself: function() {
Object.assign(this.E, {R: this.S, L: this.N});
Object.assign(this.W, {R: this.N, L: this.S});
Object.assign(this.N, {R: this.E, L: this.W});
Object.assign(this.S, {R: this.W, L: this.E});
}
};
obj.initMyself();
delete obj.initMyself;
return obj;
}());
console.log(orientations.E.R === orientations.S);