You can't.
This is just how the literal object declaration is designed in JavaScript; no self referencing will work
All references to this
are resolved based on the scope in which the code is running, defaulting to window
if you're in the global scope.
Update
Without resorting to the new getters and setters syntax, you can do two things:
Build the object but leave out the properties that have self references; afterwards, append them. This is what you did as well.
Turn a property into a function instead:
var a = {
"a" : "Hey",
"b" : function() { return this.a + "!"; }
};
console.log(a.b()); // "Hey!"
Be careful with this approach, because changing the value of a.a
will also affect the output of a.b()
.