You cannot submit “links” that are rated like this. Expressions are evaluated before appointment.
let obj = {
a: 1,
b: 2
};
obj.c = 3 + obj.b;
console.log(obj.c);
> 5
obj.b = 10;
console.log(obj.c);
> 5
If you really wanted to do something like this, you can create a new type of object with a form of lazy evaluation.
function Lazy(value) {
this.value = value;
}
Lazy.prototype.valueOf = function() {
if (typeof this.value === 'function') {
return this.value();
}
return this.value;
};
Lazy.prototype.toString = function() {
return this.valueOf() + "";
}
let obj = {
a: new Lazy(1),
b: new Lazy(2)
};
obj.c = new Lazy(() => 3 + obj.b);
console.log(obj.c.toString());
obj.b = 10;
console.log(obj.c.toString());
Run codeHide result source
share