Skip to content

Instantly share code, notes, and snippets.

@coltpini
Last active August 1, 2016 04:27
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 coltpini/46b9f3d269fb0d1ab489976e59e7238d to your computer and use it in GitHub Desktop.
Save coltpini/46b9f3d269fb0d1ab489976e59e7238d to your computer and use it in GitHub Desktop.
inheritance in interesting object pattern
let MyObj = {
init(name){
let myObj = {};
myObj.greet = function(){MyObj.greet(name)};
myObj.depart = function(){MyObj.depart(name)};
return myObj;
},
greet(name){
console.log(`Hi there, ${name}!!`);
},
depart(name){
console.log(`Goodbye, ${name}!!`);
}
};
let MyObjChild = {
init(name){
let myObjChild = MyObj.init(name);
myObjChild.greet = function(){MyObjChild.greet(name)};
return myObjChild;
},
greet(name){
console.log(`Cowabunga, ${name}!!`);
}
};
let myObj = MyObj.init('Billy');
myObj.greet();
myObj.depart();
// Hi there, Billy!!
// Goodbye, Billy!!
let myObjChild = MyObjChild.init('Jimmy');
myObjChild.greet();
myObjChild.depart();
// Cowabunga, Jimmy!!
// Goodbye, Jimmy!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment