Last active
December 11, 2015 03:28
-
-
Save stephenmelrose/4537518 to your computer and use it in GitHub Desktop.
Prototypical inheritance
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
function MyObject() {} | |
MyObject.prototype.doSomething = function() { | |
console.log('MyObject.doSomething()'); | |
} | |
// ----- | |
var parent = MyObject; | |
function MyExtendedObject() { | |
parent.apply(this, arguments); | |
} | |
MyExtendedObject.prototype = Object.create(parent.prototype); | |
MyExtendedObject.prototype.doSomething = function() { | |
parent.prototype.doSomething.apply(this, arguments); | |
console.log('MyExtendedObject.doSomething()'); | |
} | |
// ----- | |
var parent = MyObject; | |
function AnotherExtendedObject() { | |
parent.apply(this, arguments); | |
} | |
AnotherExtendedObject.prototype = Object.create(parent.prototype); | |
AnotherExtendedObject.prototype.doSomething = function() { | |
console.log('AnotherExtendedObject.doSomething()'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment