Simple classical inheritance in JavaScript

This article was originally published in my blog (affectionately referred to as blargh) on . The original blog no longer exists as I've migrated everything to this wiki.

The original URL of this post was at Hopefully that link redirects back to this page.

Note that this article was written long before things like "class" were available in JavaScript.

Inheritance in JavaScript is kind of tricky. JavaScript is object-oriented-ish, meaning most things are objects. But it's also functional-ish, meaning that at times it behaves like a functional language.

My personal preference is that when I need to use inheritance, I use prototypal inheritance, since that's what JavaScript was made for. Kind of. The prototype chain in JavaScript is a bit wonky, and I shan't write about it here as it's been talked about by people far smarter than myself. And I'm not going to pretend to understand every aspect of it.

But on occasion, I find the desideratum is actually classical inheritance. There is a plethora of "classical inheritance in JavaScript made E-Z" implementations out there, but they're generally insanely complicated.

Specifically, I wanted two things:

  1. Traits
  2. Call base/parent/super from an overridden method/constructor

By traits, I mean I wanted to be able to import a collection of functions into multiple objects.

Here's what I came up with, in 300 bytes (minified):

function extend(trait, func) {
	func.$super = func.$super || {};
	for (var i in trait.prototype) {
		func.prototype[i] = trait.prototype[i];
		if (typeof(trait.prototype[i]) === "function") {
			func.$super[i] = trait.prototype[i];
	if (!func.$parents) {
		func.$parents = [];
		func.$parent = trait;
	func.$override = function(funcToOverride, newImplementation) {
		func.$super[funcToOverride] = func.prototype[funcToOverride];
		func.prototype[funcToOverride] = newImplementation;

This works in all browsers that I tested, which would be IE6+, Firefox, Chrome and Opera. Here's an example illustrating the inheritance chain:

function BaseClass(name) { = name || "world";
BaseClass.prototype.sayHello = function sayHelloBase(message) {
	console.log("Hello, " + + (message ? ": " + message : "") + " from BaseClass");

function DerivedClass(name) {
	DerivedClass.$, name);
extend(BaseClass, DerivedClass);
DerivedClass.$override("sayHello", function sayHelloWithSalutation(message, salutation) {
	message = message || "";
	message += " from DerivedClass";
	if (!salutation) {
		DerivedClass.$, message);
	} else {
		console.log(salutation + ", " + + (message ? ": " + message : ""));

function SayHelloToBilly() {
	SayHelloToBilly.$, "billy");
extend(DerivedClass, SayHelloToBilly);
SayHelloToBilly.$override("sayHello", function sayHelloToBilly(message, salutation) {
	message = message || "";
	message += " from SayHelloToBilly";
	SayHelloToBilly.$, message, salutation);
new BaseClass().sayHello();
var derived = new DerivedClass("Dawg");
derived.sayHello("a message", "Yo");
var billy = new SayHelloToBilly();
billy.sayHello("how are you", "Welcome");
console.groupEnd();"SayHelloToBilly definition");

And here's the output:

extend output

I'm not pretending this is a super excellent rendition of classical inheritance in JavaScript: there are surely better ones out there (John Resig has one that I've used, Dean Edwards also has one as part of base2). But if you need something quick, dirty and lightweight, this should do nicely.