Created
March 6, 2014 15:09
-
-
Save sbstp/9391754 to your computer and use it in GitHub Desktop.
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
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