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

1

There are 1 best solutions below

0
On

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 invokes AA.a's setter, it doesn't create the property BB.a.

The relevant part of the spec is section 9.1.9.2 OrdinarySetWithOwnDescriptor.

Demo:

var AA = {
  set a(value) {
    console.log('AA', 'value:', value, 'this:', this === AA ? 'AA': 'BB');
  }
}
AA.a = 21;
var BB = Object.create(AA);
BB.a = 42;
console.log(BB.a);