Object.defineProperties()使用方法


Object.defineProperties()使用方法

var book={};
Object.defineProperties(book,{
    _year:{
        writable:true,
        value:2004
    },
    edition:{
        writable:true,
        value:1
    },
    year:{
        get:function(){
            return this._year;
        },
        set:function(newValue){
            if(newValue>2004){
                this._year=newValue;
                this.edition+=newValue-2004;
            }
        }
    }
});
book.year=2005;
console.log(book.edition);//2

转载自:https://blog.csdn.net/yangyuqingabc/article/details/82873568

You may also like...