[javascript学习指南]Javascript 访问器属性的例子

时间:2019-08-31  来源:php常用代码  阅读:

例子

 代码如下

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;
console.log(book.edition);

半年来的话,修改_year,输出值改变的只有year,但通过set,在修改year时edition也同时改变了。

[javascript学习指南]Javascript 访问器属性的例子

http://m.bbyears.com/jiaocheng/65323.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新