返回列表

getter/setter示例

默认分类 2017/12/28 05:57

代码如下:

var book = {
  _year = 2004
};
Object.defineProperty(book, 'year', {
  get: function() {
    return this._year
  },
  set: function(newValue) {
    debugger
    this._year = newValue
  }
})
book.year = 2005
alert(book.year); //2005