Created
May 1, 2017 20:58
-
-
Save maciejmatu/ab7fe68415c75349ae1f3c80a5786158 to your computer and use it in GitHub Desktop.
Javscript Creational Pattern
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
/* | |
* Creating new Objects | |
* https://developer.mozilla.org/pl/docs/Web/JavaScript/Guide/Working_with_Objects | |
*/ | |
let myObj = new Object(); | |
let myObj = {}; // preffered method | |
let myObj = Object.create(Prototype, {name: 'Foo'}); | |
/** | |
* Adding properties | |
*/ | |
// Preferred method | |
myObj.property1 = 'foo'; | |
// Allows to use special characters in property names (e.g. '-') | |
myObj['property2'] = 'bar'; | |
// Allows to specify additional information about property | |
// such as enumerable, configurable, writable | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty | |
Object.defineProperty(myObj, 'property3', { | |
value: 'baz', | |
enumerable: true | |
}); | |
// Similar to defineProperty, but allows to define multiple properties | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties | |
Object.defineProperties(myObj, { | |
'property4': { | |
value: 'foo', | |
writable: true | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment