Created
March 5, 2014 19:31
-
-
Save sbstp/9374755 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
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