码迷,mamicode.com
首页 > 其他好文 > 详细

访问器属性:setter()函数和getter()函数

时间:2017-04-20 16:55:24      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:ret   特征   object   his   property   style   rabl   div   set   

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

 

访问器属性:setter()函数和getter()函数

标签:ret   特征   object   his   property   style   rabl   div   set   

原文地址:http://www.cnblogs.com/n2meetu/p/6739491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!