Skip to content

Instantly share code, notes, and snippets.

@cspickert
Created February 14, 2012 14:32
Show Gist options
  • Save cspickert/1827107 to your computer and use it in GitHub Desktop.
Save cspickert/1827107 to your computer and use it in GitHub Desktop.
How to subclass in JS
// 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