const o = {};
o.__defineSetter__("value", function (val) {
this.anotherValue = val;
});
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5Object.prototype.__defineSetter__() / notebook
Object.prototype.__defineSetter__()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const o = {
set value(val) {
this.anotherValue = val;
},
};
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5const o = {};
Object.defineProperty(o, "value", {
set(val) {
this.anotherValue = val;
},
configurable: true,
enumerable: true,
});
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5