Prototypal nature of JavaScript

Hated by many, loved by others. JavaScript is undeniably one of those troubled languages. The fact that it’s implementation and support is not compatible across different web browser makes it even worse and possibly hated by web developers. I would like to look closer into one of the JavaScript features, prototyping.

Some facts

JavaScript is object oriented language. It is “so much” object oriented that even Functions are first-class objects. It could be a surprise for a programmer (like myself) who used to classical object oriented approach, used in Java or C#.

It means that Functions can have properties, another functions, objects etc. Functions could be use to change state of the object or make calculations. They could also be used to create another Functions based on original.

Classes and Prototypes

Classical object oriented language is using types of object, or classes to describe it. In Prototypes one object is describing other, it is almost comparable to cloning.

To make it easier to understand I’d like to use this example:

  1. Using classical approach if I asks someone to make me another Aston Martin, I would be asked to get technical specs for it so it could be build it.
  2. Using prototypal approach if I ask for Ferrari I will be asked to bring one so they can look at it and build it.

I can define my object and if I need another copy of it I just use prototype to get the same one. In JavaScript world it also means that once I got a bunch of objects that have the same prototype, if I add new property to it I will be able to use it in all of them.

All objects have one common prototype that is Object prototype. Prototype for functions is Function prototype. One useful object method to determine if some object’s property is its own or inherited from its prototype is hasOwnProperty method.

Few examples

To create new Object in JavaScript we don’t need to describe it, just create it:

var myObject = { fooProperty: ‘some value’, barProperty: 2 };

Now if we would like another copy of it we could type something like this:

var Tmp = function(){};
Tmp.prototype = myObject;
var newObject = new Tmp();

You might wonder why is this necessary, looks messy and is not clear. Simple construction :

var newObject = {};
newObject.prototype = myObject;

should work. The reason that it is not is because in first line you are creating the object. Once it’s created it’s got Object as prototype its prototype. Adding another prototype to it’s chain of prototypes will not automatically make it inherit all the properties of another prototype.

Adding new property to prototype could be done using this contruction:

var newObject = {}; = “some text”; = function(){
// do something

Multiple prototypes, chain of prototypes

It is impossible to define multiple prototypes for an object.

var objectA = { foo: ‘blah’ };
var objectB = { bar: ‘blah two’ };

var Tmp = function(){};
Tmp.prototype = objectA;
Tmp.prototype = objectB;
var newObject =;

In here newObject will only inherit properties of a last assign prototype and it would be objectB.

If you want to inherit multiple prototypes you have to get Function to help you.

function A(){ = ‘blah’;
function B(){ = ‘another blah’;
B.prototype = new A();
var c = new B();

Little help from jQuery

jQuery has a method that helps us to extend one object with some properties from another. Here how you can use it:

function A(){ = ‘some text’;
var anotherObject = { bar: 2 };
$.extend(A.prototype, anotherObject);


Cheers, Greg

Knights tale of Ruby and Rails

GiguI should be painting the office (one room in our house that is working as a storage room at the moment), but I can’t force myself to start. I’m too lazy to paint but I thought I write about my experience with Ruby and Rails, or in other words, my first steps as a ROR developer.

Yada Yada Yada

Let’s start with Ruby, new programming language for me. So, I don’t need to compile anything to run, it’s interpreted. That’s cool, I used to do a lot of PHP programming some days ago and I liked it a lot. On the other hand, syntax, rules and conventions were weird for me. I did have a conversation with some other developers and they shared my feelings on that subject.

Yoda Yoda Yoda

I got this strange way of learning new programming language. I just get tools and IDE for it, do a little bit of reading and off I go. So, I’m a Linux user, I’ve seen a lot of ROR applications development on Macs using TextMate or something similar. I started to look for some tool for other platform. Ruby comes bundle with Scite. It’s great that it has syntax highlighting but nothing more. For me the most important bit is InteliiSense.

There is no strong typing in Ruby so how any IDE will know what kind of object it’s dealing with. As a result completion is just a list of ALL methods and properties you can associate with anything. That’s not really helping. Oh, by the way, refactoring tools boils down to RENAME 🙂 On the other hand as soon as you know language it keeps you focus as you need to be careful about what your are typing.

Trails of Rails

When I finished with all the setup I kinked off some development. I can’t believe how fast I was able to do things with rails. For a basic web application these framework is just amazing. It comes with generator that creates all single parts of application (model, view, controller, database migration) with skeleton code, inside appropriate folders. I finally selected Netbeans to be my IDE as it seemed to have most reasonable code completion and integrated all script generation.

Book da Book

I can’t use some clever code completion tricks, bottom line: reading books. There are two titles recommended for Ruby and Ruby on Rails by a lot of people (can’t really name a single person at the moment) in Pragmatic Programmers Bookshelf. I can’t say that any of them give me a good kick start. First chapters are dragging for ages and once finished I had impression that I didn’t learn anything. Those books are good language/framework reference though with much better content after first chapter.

Summer (y)

When I started things looked a little bit messy. Strange syntax, no good IDE. After few days of development I started to like it. Language features stopped being annoying and there is more visible results. I will share more experience after few more weeks of development. Stay tuned.