Last active
August 1, 2016 04:27
-
-
Save coltpini/46b9f3d269fb0d1ab489976e59e7238d to your computer and use it in GitHub Desktop.
inheritance in interesting object 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
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