Last active
December 8, 2019 03:11
-
-
Save manavm1990/d8807d03c81f4181a974f223c2421499 to your computer and use it in GitHub Desktop.
OOP Factory Fxn.
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
// Build prototype of methods | |
const proto = { | |
hello() { | |
return `Hello, my name is ${this.name}`; | |
} | |
}; | |
// Merge a new proto object along with the properties and merge them. | |
const greeter = name => | |
Object.assign( | |
/** | |
* The Object.create() method creates a new object, using an existing object as the prototype of the newly created object. | |
* (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) | |
*/ | |
Object.create(proto), | |
{ | |
name | |
} | |
); | |
console.log(greeter, "greeter"); | |
// Pump out a new 'created' person | |
const george = greeter("george"); | |
console.log(george, "george"); | |
console.log(george.hello()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment