One way for your code to be able to access 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);
The simple rule is, when you have obj.fn(), then inside of fn()'s code, the this is bound to obj. But if you have g = obj.fn; and then g(), the code inside of g() will have this bound to the global object.
And your requirement also may have the "self" references. You are setting E.R to refer to S but S doesn't exist yet. So that's why my code created them first, and then the Object.assign() pretty much just copy the properties over.
Object.assign(this.E, {R: this.S, L: this.N});
is just
this.E.R = this.S;
this.E.L = this.N;
One way your code could have been is:
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);