Skip to content

Instantly share code, notes, and snippets.

@kbaird
Created August 8, 2012 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbaird/3297780 to your computer and use it in GitHub Desktop.
Save kbaird/3297780 to your computer and use it in GitHub Desktop.
Simple JavaScript subclassing
/*
* Simple JS subclassing, shamelessly stolen from
* http://kevinoncode.blogspot.com/2011/04/understanding-javascript-inheritance.html
*/
function Super() {
this.value = 42;
}
function Sub(parent) {
function SubClass() {};
SubClass.prototype = parent;
var child = new SubClass();
// whatever subclass-specific behavior...
return child;
}
s1 = new Super();
s2 = new Sub(s1);
s2.value; // returns 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment