So it must be good right?
Well… here’s where my story begins about what I don’t like about Prototype JS. But first, take a look at the source code for prototype 1.6.0. You don’t have to read it all, just glance at it and scroll through the file a bit. That’s a lot of code right?
But that’s not all of it. Just having a large API does not make prototype evil. But prototype is monolithic. It’s not a large set of modules in one file, though it may look like that. It may even be stored like that in CVS. But it’s not.
Some pieces of code may be worse than others, but the pieces of code that I have personally tried to pull out of prototype were very interwoven with the rest of prototype.
For example prototype tends to ‘enhance’ DOM objects and arrays using functions $() and $A(). Those functions then add some extra methods to otherwise native types. Those enhanced methods are happily called everywhere, but they are prototype specific.
Then there is the Prototype object that contains a load of useful properties and functions. Those are used throughout the code. Very convenient, but it makes all that code dependent on that object. A lot of times, the offending line of code could easily be replaced by an only marginally longer line of code that avoided the dependency.
The same goes for a lot of functions that prototype adds to native objects. Such as Object.isFunction(), which just returns typeof object == “function”. Ok so your checks become a teeny bit shorter and more readable. But it makes your code dependent on a function that is only available in prototype. It’s the spirit of coding that prototype was built with: no duplicate code. That’s good to some degree, but if it comes at the expense of modularity, I choose modularity. It’s the old story of tight cohesion, loose coupling. Prototype is tightly cohesive as a whole. I think they should have separated the code more into modules and introduce loose coupling between the modules. As it stands now, it’s monolithic.
And this monolithicness seems to be contageous. Code gets infected by it. Hey if you want to use function A from prototype, you need to include all of it, so you get function B for free as well. And since it’s there.. why not use it, right? And then, if your code is useful, why not include it right into prototype? This way, the framework never stops growing…
Prototype viral? Well that may be pushing it, but just read this article about pimping some code for inclusion into prototype that I think illustrates what I’m saying here, and draw your own conclusions.
I’ve given up on trying to make protoype modular. It’s just too interwoven for me to get any joy out of doing that. So instead I’m trying to build up my own personal framework of scripts I like using Packages JS. Give it a try!