Javascript Object Oriented Design - Javascript Multiple Properties








To define multiple properties on an object, use Object.defineProperties() instead of Object.defineProperty().

This method accepts two arguments: the object to work on and an object containing all of the property information.

Example

The following code defines two properties:


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