Skip to content

Instantly share code, notes, and snippets.

@maciejmatu
Created May 1, 2017 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maciejmatu/ab7fe68415c75349ae1f3c80a5786158 to your computer and use it in GitHub Desktop.
Save maciejmatu/ab7fe68415c75349ae1f3c80a5786158 to your computer and use it in GitHub Desktop.
Javscript Creational Pattern
/*
* 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