var book={};
Object.defineProperties(book,{
_year:{
value:2004
},
edition:{
value:1
},
year:{
get:function () {
return this._year;
// body...
},
set:function(newValue){
if (newValue>2004) {
this._year=newValue;
this.edition+=newValue-2004;
}
}
}
});
book.year=2005;
console.log(book.year);![图片描述][1]
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
通过Object.defineProperties定义对象属性,如果没有配置 writable: true 的话默认writable:false`_year:{
},`
这样就可以配置_year属性可写入
通过
defineProperty定义的对象属性默认是不可写的。要想能够改变属性值,需要添加writable: true。