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/11/14 13:09:59

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
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);

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