Skip to content

Instantly share code, notes, and snippets.

@sbstp
Created March 5, 2014 19:31
Show Gist options
  • Save sbstp/9374755 to your computer and use it in GitHub Desktop.
Save sbstp/9374755 to your computer and use it in GitHub Desktop.
function extend(parent, child) {
var proto = {};
for (fnName in parent.prototype) {
proto[fnName] = parent.prototype[fnName];
}
for (fnName in child.prototype) {
proto[fnName] = child.prototype[fnName];
}
child.super = parent;
child.prototype = proto;
}
// parent
function Parent(name) {
this.name = name;
}
Parent.prototype.say = function () {
console.log('Parent! ' + this.name);
}
// child
function Child(name) {
Child.super.call(this, name);
}
Child.prototype.say = function () {
console.log('Child! ' + this.name);
}
extend(Parent, Child);
var parent = new Parent("bob");
parent.say();
console.log(parent);
var child = new Child("ann");
child.say();
console.log(child);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment