Javascript多属性

2018-01-06 19:14 更新

Javascript面向对象设计 - Javascript多属性


要在对象上定义多个属性,使用Object.defineProperties()而不是Object.defineProperty()。

此方法接受两个参数:要处理的对象和一个包含所有属性信息的对象。

例子

以下代码定义了两个属性:


var book1 = {}; 
/*  w w  w . j av a 2 s. c o m*/
Object.defineProperties(book1, { 
    // data property to store data 
    _name : { 
        value : "Javascript", 
        enumerable : true, 
        configurable : true, 
        writable : true 
    }, 
    // accessor property 
    name : { 
        get : function() { 
            console.log("Reading name"); 
            return this._name; 
        }, 
        set : function(value) { 
            console.log("Setting name to %s", value); 
            this._name = value; 
        }, 
        enumerable : true, 
        configurable : true 
    } 
}); 


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号