Crockford vs Base2

Last modified Feb. 11, 2008 | Revision 38

Useful Note: A good piece of code to see some of the ideas Douglas Crockford encourages (although not so much of the prototypal stuff) in JS programming can be found in the codemirror parser files:

Onward with the original discussion

Greg and brehaut are wondering about the various pros and cons of Dean Edward’s Base2 libraries implementation of classical inheritance in JS and Douglas Crockfords prototypal inheritance model.

mattw doesn’t really understand how to use prototype for inheritance, having not ever played with it before. Maybe you could start by summarising each approach?

Have a read of this and you’ll know about as much as me. Basically you create a ‘protoype object’ and then you create instances of that object (using crockford’s object() function), each of which can be appended to/modified/hacked however you please.
The thing that I don’t get is how is it better than just creating classes (or at least, their js approximation) and using new to create instances (which can be modified in the same way, as far as I know.) – greg

My initial thoughts would be that this allows an instance to hack its ‘class’ without messing with other instances. In classical inheritance, changing a class changes it for all instances, which would sometimes be desired behaviour, but not always. That said, I need to play with this some more. — mattw

Hmmm… but I don’t see why you can’t do that anyway, once you created an instance. If I have a class Test whose prototype has a property prop and I go:

var a = new Test();  
a.prop = 1;

It won’t affect any other instances of that class… will it?
greg

As I understand it, the point of prototypes is that you dont have to define a class, you can just make any object a more specialised version of an existing object. It begins to make more sense when you stop trying to think in terms of constructors. I’ll try to put an example up later. – brehaut

{ hocus example deleted – brehaut }

January 08

I’ve been thinking and reading more code in the prototypal style recently. I am in agreement that the new keyword is b0rken, it is very hard to use the constructor function idiom in javascript with prototype chaining in a way that doesn’t break at some point, and inevitable some trick or hack will be needed, be it an init method or something more sophisticated like base.

A little epiphany I had recently while reading Crockford’s Beautiful Code chapter, includes the concept of Null Object into the prototypal model. For any given interface / collection of related objects, you initially define a primitive literal that you can later inherit from. I am going to suggest that if there is no obvious root object, that object should become the null object for the hierarchy.

An additional idea that needs some testing is using prototyping ot create chains of non-mutable objects when programming functionally. This way, you use the prototyping to refer back to the previous record for all the members that have not been modified, and the new values are on the new object. This of course assumes that all code associated with the data treats it as non-mutable.

brehaut

An example

Heres is my first real prototypal piece of code; Color.js its not 100% complete, but i think mostly there.

11th Feb 08

mattw ran across this; looked interesting:

Comments?

That looks like solid advice, and definitely a nice clear discussion of the forces involved. – brehaut

Last modified Feb. 11, 2008 | Revision 38