Last active
November 23, 2015 01:03
-
-
Save jsec516/bedaf558feae43828d0e to your computer and use it in GitHub Desktop.
Different ways to create javascript object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Each of the following options will create a new empty object: | |
var newObject = {}; // or | |
var newObject = Object.create(null); // or | |
var newObject = new Object(); | |
// four ways in which keys and values can then be assigned to an object | |
// ECMAScript 3 compatible approaches | |
// 1. Dot syntax | |
newObject.someKey = 'Hello World'; // Write properties | |
var key = newObject.someKey; // Access properties, only useful for valid property name | |
// 2. Square bracket syntax | |
newObject['someKey'] = 'Hello World'; // Write properties | |
var key = newObject['someKey']; // Access properties | |
// ECMAScript 5 only compatible approaches | |
// For more information see: http://kangax.github.com/es5-compat-table/ | |
// 3. Object.defineProperty | |
Object.defineProperty(newObject, "someKey", { | |
value: "for more control of the property's behavior", | |
writable: true, | |
enumerable: true, | |
configurable: true | |
}); | |
// If the above feels a little difficult to read, a short-hand could | |
// be written as follows: | |
var defineProp = function ( obj, key, value ){ | |
config.value = value; | |
Object.defineProperty(obj, key, config); | |
} | |
// Create a new empty object | |
var man = Object.create(null); | |
// Populate the object with properties | |
defineProp( man, 'car', 'Delorean' ); | |
defineProp( man, 'dob', '1981' ); | |
defineProp( man, 'beard', false ); | |
// 4. Object.defineProperties | |
Object.defineProperties(newObject, { | |
"someKey": { | |
value: "Hello World", | |
writable: true | |
}, | |
"anotherKey": { | |
value: "Foo bar", | |
writable: false | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment