getter和setter只适用于ES5中的基元值吗



getter和setter是否只适用于ES5中的基元值?

var foo = { 
  get bar() {
    return this._bar;
  },
  set bar(value) {
    this._bar = value;
  } 
}
foo.bar = function() {}; //appears to overwrite the property rather than assign the value of _bar

是什么让你认为它覆盖了属性?

var foo = { 
  get bar() {
    console.log('the getter is still here');
    return this._bar;
  },
  set bar(value) {
    this._bar = value;
  } 
}
foo.bar = function() { console.log('xxx'); };

运行foo.bar()时,输出为:

getter仍然在这里
xxx

最新更新