Geeks With Blogs
Phubar Baz Musings of an HTML5 Programmer

A while back I wrote about Javascript inheritance, trying to figure out the best and easiest way to do it ( That was 2 years ago and I've learned a lot since then. But only recently have I decided to just leave classical inheritance behind and embrace prototypal inheritance.

For most of us, we were trained in classical inheritance, using class hierarchies in a typed language. Unfortunately Javascript doesn't follow that model. It is both classless and typeless, which is hard to fathom for someone who's been using classes the last 20 years. For the last two or three years since I've got into Javascript I've been trying to find the best way to force it into the class model without much success. It's clunky and verbose and hard to understand.

I think my biggest problem was that it felt so wrong to add or change object members at run time. Every time I did it I felt like I needed a shower. That's the 20 years of classical inheritance in me. Finally I decided to embrace change and do something different. I decided to use the factory pattern to build objects instead of trying to use inheritance.

Javascript was made for the factory pattern because of the way you can construct objects at runtime. In the factory pattern you have a factory function that you call and tell it to give you a certain type of object back. The factory function takes care of constructing the object to your specification.

Here's an example. Say we want to have some shape objects and they have common attributes like id and area that we want to depend on in other parts of your application. So first thing to do is create a factory object and give it a factory method to create an abstract shape object. The factory method builds the object then returns it.

var shapeFactory = {
getShape: function(id){
var shape = {
id: id,
area: function() { throw "Not implemented"; }
return shape;

Now we can add another factory method to get a rectangle. It calls the getShape() method first and then adds an implementation to it.

getRectangle: function(id, width, height){
var rect = this.getShape(id);
rect.width = width;
rect.height = height;
rect.area = function() { return this.width * this.height; };
return rect;

That's pretty simple right? No worrying about hooking up prototypes and calling base constructors or any of that crap I used to do. Now let's create a factory method to get a cuboid (rectangular cube). The cuboid object will extend the rectangle object. To get the area we will call into the base object's area method and then multiply that by the depth.

getCuboid: function(id, width, height, depth){
var cuboid = this.getRectangle(id, width, height);
cuboid.depth = depth;
var baseArea = cuboid.area;
cuboid.area = function()
var a =;
return a * this.depth;
return cuboid;

See how we called the area method in the base object? First we save it off in a variable then we implement our own area method and use call() to call the base function.

For me this is a lot cleaner and easier than trying to emulate class hierarchies in Javascript.

Posted on Monday, November 5, 2012 10:15 AM | Back to top

Comments on this post: Javascript Inheritance Part 2

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © PhubarBaz | Powered by: