javascript 倒数第三行为什么是book.year而不是book._year.求详细点解释var book = {_year : 2004 , edition : 1}Object.defineProperty(book , "year" , {get:function(){return this._year;},set:function(newValue){if(newValue > 2004){this._yea

来源:学生作业学帮网 编辑:学帮网 时间:2024/05/14 20:23:04

javascript 倒数第三行为什么是book.year而不是book._year.求详细点解释
var book = {
_year : 2004 ,
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 = 2006;
alert(book.edition);

Object.defineProperty方法的作用是:将属性添加到对象或修改现有属性的特性
这里也就是为book新加了一个year属性;
this._year = newValue;重新给_year赋值:2006
this.edition += newValue - 2004;
1+=2006-2004
结果是3