Skip to content

Instantly share code, notes, and snippets.

@manavm1990
Last active December 8, 2019 03:11
Show Gist options
  • Save manavm1990/d8807d03c81f4181a974f223c2421499 to your computer and use it in GitHub Desktop.
Save manavm1990/d8807d03c81f4181a974f223c2421499 to your computer and use it in GitHub Desktop.
OOP Factory Fxn.
// 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