Last active
December 17, 2015 05:49
-
-
Save t0dd/5560893 to your computer and use it in GitHub Desktop.
JS - Using the defineProperty() option available in EcmaScript 5
This file contains 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
//Example 1 | |
var createPerson = function(firstName, lastName){ | |
var person = {}; | |
//Descriptor object - define single property | |
//2 types - Data & Accessor descriptors | |
Object.defineProperty(person, "firstName", { | |
//data descriptor | |
//immutable unless you set the writable descriptor | |
value: firstName | |
}); | |
Object.defineProperty(person, "lastName", { | |
value: lastName | |
}); | |
return person; | |
}; | |
var person = createPerson("Jack", "Foo"); | |
/**************************************************************************/ | |
//Example 2 | |
var createPerson = function(firstName, lastName){ | |
var person = {}; | |
//Define multiple properties | |
Object.defineProperties(person, { | |
firstName : { | |
value: firstName, | |
writable: true | |
}, | |
lastName : { | |
value: lastName, | |
writable: true | |
}, | |
//Accessor descriptors | |
fullName: { | |
get: function() {return this.firstName + " " + this.lastName; | |
}, | |
set: function(value) { | |
this.firstName = value; | |
this.lastName = value; | |
} | |
} | |
}); | |
return person; | |
}; | |
var person = createPerson("Jack", "Foo"); | |
/*************************************************************************** | |
Example 3 | |
*/ | |
var createPerson = function(firstName, lastName){ | |
var person = {}; | |
//Using the configurable option | |
//leaves object open for altering - not optimal | |
Object.defineProperties(person, { | |
firstName : { | |
value: firstName | |
}, | |
lastName : { | |
value: lastName | |
}, | |
//Accessor descriptors | |
fullName: { | |
get: function() {return this.firstName + " " + this.lastName; | |
}, | |
configurable: true | |
} | |
}); | |
return person; | |
}; | |
var person = createPerson("Jack", "Foo"); | |
Object.defineProperty(person, "fullName", { | |
get: function(){return this.lastName + ", " +this.firstName;} | |
}); | |
/***************************************************************************** | |
Example 4 | |
*/ | |
var createPerson = function(firstName, lastName){ | |
var person = {}; | |
//Using the enumerable option | |
Object.defineProperties(person, { | |
firstName : { | |
value: firstName, | |
enumerable : true | |
}, | |
lastName : { | |
value: lastName, | |
enumerable : true | |
}, | |
//Accessor descriptors | |
fullName: { | |
get: function() {return this.firstName + " " + this.lastName; | |
}, | |
enumerable : true | |
} | |
}); | |
return person; | |
}; | |
var person = createPerson("Jack", "Foo"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment