Created
February 14, 2012 14:32
-
-
Save cspickert/1827107 to your computer and use it in GitHub Desktop.
How to subclass in JS
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
// Source: http://www.golimojo.com/etc/js-subclass.html | |
function subclass(constructor, superConstructor) | |
{ | |
function surrogateConstructor() | |
{ | |
} | |
surrogateConstructor.prototype = superConstructor.prototype; | |
var prototypeObject = new surrogateConstructor(); | |
prototypeObject.constructor = constructor; | |
constructor.prototype = prototypeObject; | |
} | |
function A (arg) | |
{ | |
this.arg = arg; | |
} | |
function B (arg) | |
{ | |
A.call(this, arg); | |
this.print = function () { | |
console.log(this.arg); | |
}; | |
} | |
subclass(B, A); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment