javascript数据属性和访问器属性的理解

 1 var book={};
 2 Object.defineProperties(book,{
 3     _year:{
 4         value:2014
 5     },
 6     edition:{
 7         value:1
 8     },
 9     year:{
10         get:function(){
11             return this._year;
12         },
13         set:function(newValue){
14             if(newValue>2014){
15                 this._year=newValue;
16                 this.edition+=newValue-2014;
17             }
18         }
19     }
20 });

这种写法与C#的属性,字段很像。数据属性_year,edition相当于字段,访问器属性year相当于属性。

 

posted on 2014-12-11 16:18  菜地守护者@马鞍山  阅读(157)  评论(0)    收藏  举报

导航