Nested inheritance in Javascript

Most examples of javascript inheritance only go one level deep (for example, Student inherits (→) from Person). That’s all well and good, but what if you have a long chain for things you want to inherit from?

Say we have A → B → C → D → E, and each has its own methods and attributes. How can they inherit from each other cleanly?

Here is the initial solution:

So, what’s special about the above. Not much… except the the only gotcha is the ordering of:

//you need to make sure you don't override your functions
//Good:
X.prototype = new Y();
X.prototype.foo = function(){ .... };

//Bad (trashes foo!)
X.prototype.foo = function(){ .... };
X.prototype = new Y();

Adding methods and properties

Once we have a chain, we naturally want to make objects created from that chain do something useful. The above A, B, C, etc. is a little academic, so lets now use a more “real world” example.

Lets say we have the following inheritance chain of vehicle types:

Vehicle → LandVehicle → Car → SportsCar → RacingCar

We assume that all Vehicles have a “registration” property that is unique for each vehicle we create:

The problem when we run the above is that the registration is not unique! When we are constructing the new RacingCar(), we are using the same prototype for all instances (in fact for any kind of Vehicle).

To solve this problem, we need to make sure that for “this” instance we are given a unique “registration”. To do that, we need to “call()” the function from which we are inheriting from using “this“, like so:

function RacingCar(){Vehicle.call(this)}

So, that makes sure that “this” (whatever that is associated with) gets the right “registration” number (i.e., a random number):

Ok, so far so good… but if we start adding methods and unique properties on each level, we are going to have the same problem. So what we can do is just cascade “this” through the inheritance chain:

function LandVehicle(){Vehicle.call(this)}
function Car(){LandVehicle.call(this)}
function SportsCar(){Car.call(this)}
function RacingCar(){RacingCar.call(this)}

Now we can start individualising each object to our needs. The following shows a complete set of custom objects that inherit from each other. It also shows “static” functions and property being declared (e.g., the make of a Car and getting a randomMake() for either a bike or car):

So there you have it. Classical inheritance JavaScript styles.

Dynamic function names in JavaScript

Creating a function with a custom name is not something you can do easily out of the box in ECMAScript. Thankfully, ECMAScript comes with a custom function constructor.

First, the basic code, which will give most of you want you want:

The above will create an anonymous function, which when called creates the named function (using the name variable). This functionality is a good substitute for when you can’t use eval() but you need a function with a custom name. Eval is generally useless in ES5 strict mode for a number of good reasons, so its best avoided.

However, an issue with the code above is that we had to write the main code into a string. This sucks because it makes it difficult to debug (i.e., we can’t easily set break points), so we want to avoid that as much as possible. The solution is to make the main function external, pass it as an argument to the Function constructor to call it.

Another advantage here is that we can set private variables inside our custom function, which can then be mixed with public arguments. Check this out!:

You can basically the start doing cool things from there, like:

The above is mega useful for dynamically implementing custom DOM-like interfaces… like shims. Enjoy! :)

Steve Jobs: the story of a sociopath

Steve Jobs, Book, by Walter IsaacsonI’ve read other semi-biographies of Jobs and, working with a lot of people in the tech industry, I’d heard many of the stories of how much of a bastard he was. However, this biography left me quite bemused and surprised: I never expected Jobs to be such a disgustingly-shameless-sociopath-brat-cry-baby! even to the last minute, with Jobs having to have control over the cover image of the book.

I don’t think he would have liked much of what is inside this book; which is what makes it great.

It’s amazing that Jobs sought out Isaacson to write this biography. And Isaacson, pre-warning Jobs that he was going to uncover all the dirt, delivers a very inhuman story. In Isaacson other book on Einstein, he also revealed Albert’s many flaws and brought Albert down to our level. With this book, he absolutely devastates the image of Jobs as a great business leader and as human being: from his stinky hippy days, to his denial of his daughter Lisa and smear campaign of the mother, to his tyrannical and plainly mean way he constantly ripped-off and mistreated other people.

I guess Job’s own reflection of his life must have been also distorted by his “reality distortion field”. It’s great that this book came out when it did. If anything, it shows that Steve Jobs was not in the same league as other great inventors and geniuses of the past century. Jobs just rode on the great ideas of those around him. If it was him that made those ideas successful is unclear, so Jobs is just shown as part of the greater collective that was, and remains, Apple computers.

I anything, it’s good that Isaacson shows why no one should take inspiration from the cold, hard, tyrannical a**hole that was Steve Jobs. A great read! And proof you can’t judge a book by its cover.