
Classic Inheritance
By: a guest on
Jul 13th, 2012 | syntax:
JavaScript | size: 0.65 KB | hits: 19 | expires: Never
// Parent class, inherits from "Object"
function MyClass() {
var somethingPrivate;
var somethingElse;
// Constructor stuff
}
// Only methods on the prototype can be inherited
MyClass.prototype.someMethod = function() { /* blah blah */ }
MyClass.prototype.someOtherMethod = function() { /* blah blah */ }
// You use "new" to create objects from the prototype
var obj = new MyClass();
// Create a basic child class
function ChildClass() { /* Constructor stuff */ }
// Do the inheritance
ChildClass.prototype = new MyClass();
// Override stuff or add child-specific stuff
ChildClass.prototype.overridenMethod = function() { /* blah */ }