Skip to content

Instantly share code, notes, and snippets.

@stephenmelrose
Last active December 11, 2015 03:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenmelrose/4537518 to your computer and use it in GitHub Desktop.
Save stephenmelrose/4537518 to your computer and use it in GitHub Desktop.
Prototypical inheritance
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