1.干嘛用的?

  getter()函数:返回有效的值

  setter()函数:调用它并传入数据,这个函数决定如何处理数据

 

2.具备哪些属性?如何定义?

  configurable(默认为true),enumerable(默认为true),get(默认为undefined),set(默认为undefined) 四个属性特征

 

 

3.具体应用?

var book={_year:2017,edition:1};

Object.defineProperty(book,"year",{
    get:function(){return this._year},
    set:function(newValue){
            if(newValue>2004){
                this._year=newValue;
                this.edition+=newValue-2004
            }
    }
});

book.year=2005;
alert(book.edition);//2
alert(book._year); //2005

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2022-12-23
  • 2021-08-04
相关资源
相似解决方案