code like below :
function def(obj, key) {
var val = obj[key];
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter() {
return val
},
set: function reactiveSetter(newVal) {
val = newVal;
}
})
}
var AA = Object.create({});
AA.a = 1;
def(AA, "a")
var BB = Object.create(AA);
BB.a= 3;
BB.hasOwnProperty("a") == false // why ?
thank you
Unlike with data properties, if an object has an accessor property with a setter in it's prototype chain, that setter is invoked (in the context of that object) instead of a new property being created on the object itself.
In other words,
BB.a = 3
invokesAA.a
's setter, it doesn't create the propertyBB.a
.The relevant part of the spec is section 9.1.9.2 OrdinarySetWithOwnDescriptor.
Demo: