News

Namespacing Prototype

Sunday, January 07th, 2007

While I am pretty much now a jQuery fanboy, Prototype still has a foothold in the web, and many people still want/need to use it.

Lately, at work I have been having to provide developer access to both Prototype and jQuery.

As another proof of jQuery's awesomeness, John went ahead and namespaced jQuery's code base. What this means is that you can very easily avoid conflicts with other libraries.
Yahoo does this with YUI (and while kudos to them for doing it, I am going to post later on why I loathe the way they do it).
However, Prototype, it all it's shortsighted glory, fails to namespace. I'm not sure why, and perhaps because it is easier to use the global namespace, but it really makes using it a pain.

Until now. I have gone ahead and namespaced Prototype. The version I am using is version 1.5.0 RC2. If anyone cares, I could namespace version 1.4.0, but we'll see if anyone cares.

So what does this mean? Well now, you can easily rename any of the '$' functions so as not to conflict with other libraries.

You can grab the namespaced Prototype here. At the very bottom, you'll notice a few lines like so:
/* 
---------------------------------
Function naming
---------------------------------
*/
var $ = Prototype.$;
var $A = Prototype.$A;
var $H = Prototype.$H;
var $R = Prototype.$R;
var $F = Prototype.$F;
var $$ = Prototype.$$;

You can now easily rename just those functions without breaking the rest of the script. Now, there may be cleaner, better ways to do this, but there is already an existing Prototype object, and IMHO it makes the most sense to place those functions in there. I'd love to hear any other thoughts though...

*Update*

I've gone ahead and updated the code. You'll notice that there is a lot longer list of var declarations. Basically, this is because I utilized a method of namespacing that was mentioned on the Rails development group, by placing the majority of Prototype into an anonymous function.
The way I have done it here allows you to easily rename each of the objects, while also not breaking any existing Prototype based code.
Hope it helps :)

» Nate Cavanaugh @ 2:43 pm

2 comments · Leave one

These 2 people have opinions

Hannah Gray said:

I've been watching the JQ list and have not heard mention of Namespacing the official release - perhaps I missed something? I've just been doing it by hand, as you have done w/ProtoType. But it would be nice if there was a standard so that all the plugins didn't have to be likewise modified to support an individual's namespace scheme.

Ole Laursen said:

Do you happen to have a namespaced version 1.5.1 of Prototype? Plotr claims it needs 1.5.1 and I need to use it with several jQuery plugins.

I really hope the Prototype developers will consider namespacing Prototype upstream soon.

Share some insight

*
*
*
*
*


Listening to

  • Glen Hansard

    Trying to Pull Myself Away
  • Glen Hansard

    Fallen from the Sky
  • Benzos

    On Your Own
  • Glen Hansard

    Lies
  • Glen Hansard

    Lies