Skip to content

Instantly share code, notes, and snippets.

@stephenmelrose
Last active December 11, 2015 03:28
Show Gist options
  • 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