Latest Posts

Some advice for junior developers new on the job by Henrik on November 15th, 2017First of all, the below applies to a quite high stakes setting in a financially related company, a place were we don't move fast and break things if we want to continue being in business.

Setting up Ansible for MySQL by Henrik on September 14th, 2016In this how to we're going to manage in total 16 different LXC nodes on two different host machines.

A Function Browser for Emacs by Henrik on April 9th, 2016In my emacs init file for 2015 post I state:
However it would be kind of nice to be able to run a command to open a new buffer with links to line numbers for all definitions in the current file, shouldn’t be too hard to implement either, we’ll see if I manage in 2015.

Hacking Wordpress The Ugly And Quick Way by Henrik on August 4th, 2015Recently I've started a little project to see how I can do with IDN affiliate sites where there is a lot less competition than in English.

Functional HTML Rendering with PHP by Henrik on August 4th, 2015When you're working with a programming language that doesn't have templating per default and you're not in the mood - or don't see the need - for templating your first course of actions is to write something to obviate having to print and concatenate everything.

I think I’ve finally managed to get a handle on prototyping with JavaScript, after all that lisping and jQuerying it turned out to be a formality in the end. I have to thank Steve for finally getting my ass off the couch though.

At first I started thinking about how to do something similar to a classical OO system and I found an example, quite a lot isn’t it? It didn’t seem to rhyme very well with what Steve wrote either, about how instances inherit instances.

Let’s take a look at something very simple and also let’s start from the very beginning in case you’re really indoctrinated and find this hard to wrap your head around. Don’t sweat it if you do, I’ve had major problems too.

First of all we have to realize that a function is not just a function in the traditional sense, it is but at the same time it isn’t, it’s just that the choice of wording is horrible, anything but function would have been better. Anyway, the function keyword in JavaScript can basically function as the class word in other languages. Lets take a look at a simple example:

So the function can be instantiated, super weird I know, imagine I had written class Human… instead or something. Note also the assignment of a function to a member variable in the form of isRetired, completely legal, it seems that an unnamed function() can be assigned to a variable just like everything else. That’s why we can do the following too:

Barry = new Harry.constructor("Barry", 70); // Same as new Human("Barry", 70);
document.writeln(Barry.toStr());

Oops, “Barry.toStr is not a function”, that’s because we’re really just creating another human, since toStr was added to Harry, Barry won’t have access to this functionality. To solve this problem the famous prototype keyword can be used:

We basically added toStr() to the Human “class” here, or “prototype” in this case, that is what that initial Human function is, a prototype, or blueprint if you’re an architect. Remember the above way of creating a new Human object with the help of instance.constructor(), it will become very important shortly.

So the prototype was locally overridden like we would expect. Let’s take a look at how a simple inheritance system based on instances instead of prototyping can be constructed, if you want classical inheritance through prototyping mania, by all means, follow the above link and dig in 🙂

First of all, Object is the uber template if you will. Everything we do inherits from this base construct. That’s why we can add the above extend function to everything by prototyping it.

First we create a new object with the help of the constructor of the “parent” object, in this case a new Human, the point is that we lay the foundation of our new object with everything that has been prototyped/attached to the Human prototype/class/template/blueprint. Next we create a perfect clone of for instance Harry, all Harry’s properties will then be copied to the new object, if Harry for instance implements his own toStr() method the new object will get it too.

The second loop will in turn overwrite or add more attributes from an explicitly passed object, you’ll soon see what it looks like.

The last line lets us override the parent attribute with something we pass into the process, if not we simply use the extended instance as the parent.

The above makes the most sense since we are overwriting something very basic, in the prototype, with something more specific, in the instance. Before you run the below test make sure you have Harry’s creation line, the prototype toStr and Harry’s version of it uncommented:

So we follow up with adding another function to the Jason instance and then we create Bill from him, and of course Bill will now have the isInjured method too. In case the parent information is wanted it can of course be retrieved:

Even though we only do one parent in this example it should be evident that we can easily work with an arbitrary number of parents simply by copying their functions to the new instance in a “factory” fashion. I’ll leave that up to you to 🙂