Skip to content

Instantly share code, notes, and snippets.

@sbstp
Created March 6, 2014 15:09
Show Gist options
  • Save sbstp/9391754 to your computer and use it in GitHub Desktop.
Save sbstp/9391754 to your computer and use it in GitHub Desktop.
var util = require('util');
function extend(parent, child) {
// TODO copy old functions of the child prototype
var proto;
if (false/*Object.create*/) {
proto = Object.create(parent.prototype);
} else {
function f() {}
f.prototype = parent.prototype;
proto = new f();
}
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);
}
extend(Parent, Child);
Child.prototype.say = function () {
console.log('Child! ' + this.name);
}
var parent = new Parent("bob");
parent.say();
console.log(parent);
var child = new Child("ann");
child.say();
console.log(child);
console.log(util.inspect(Child, true, null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment