let obj = {
a:2,
b: this.a,
};
console.log(obj.a); //outputs 2
console.log(obj.b); //outputs undefined
Why can't I access one property of an object from another property within the same object? What's the way around this?
let obj = {
a:2,
b: this.a,
};
console.log(obj.a); //outputs 2
console.log(obj.b); //outputs undefined
Why can't I access one property of an object from another property within the same object? What's the way around this?
It's a scope issue. Put the b
outside of the object:
let obj = {
a:2
}
obj.b = obj.a;
console.log(obj.a);
console.log(obj.b);
You can't use a this
keyword before you've declared the variable.