Namespace Prototype in 3 easy steps

0 views
Skip to first unread message

Mislav

unread,
Dec 2, 2006, 4:04:39 PM12/2/06
to Ruby on Rails: Spinoffs
This is a followup to "The future of Prototype" thread where we
discussed namespacing among other things.

This is a recipe for easy namespacing of the whole framework within the
Prototype object.

1) Wrap everything *after* the Prototype object in an anonymous
function;
2) Sanitize: remove window checks when creating Event and Element
objects,
add "var" keyword to ObjectRange;
3) On the end of the anonymous function, copy everything to Prototype
object.


After that, prototype.js should look roughly like this:

=======================

var Prototype = {
/* usual stuff */
}

;(function(){
/* rest of the framework */
/* copying code */
}())

=======================

"Copying code" is easy:

Prototype.Class = Class
Prototype.Abstract = Abstract
Protoype... = ...

You can use Object.extend to shorten this, but you can use my eval()
hack too:

"$ $A $H $R $F Class Abstract Try PeriodicalExecuter Template \
Enumerable Hash ObjectRange Ajax Toggle Insertion Selector Form Field \
Position Element Event $break $continue".split(' ').each(function(obj){
Prototype[obj] = eval(obj)
})

Note that I write the string over 3 lines by using a backslash on the
end of the line.


So, what isn't namespaced?

1) Object.extend / inspect / keys / values / clone
2) document.getElementsByClassName / _getElementsByXPath
3) DOM extensions (Prototype applies stuff to HTMLElement etc)
4) Number, String, Array, Function prototypes are augmented

First two points can be namespaced, too, with some extra work. The
technique demonstrated here is minimal and serves only as a
proof-of-concept.

You'll have problems with 3rd party code that iterate over associative
Arrays with for-in loops. Modify these loops to check for object's
prototype:

for (var name in arr) {
if (arr.prototype[name]) continue;
/* do your stuff with arr[name] */
}

That's it. Happy namespacing,

--
Mislav

Peter Michaux

unread,
Dec 3, 2006, 1:46:04 AM12/3/06
to rubyonrail...@googlegroups.com
Hi Mislav,

On 12/2/06, Mislav <mislav....@gmail.com> wrote:
>
> This is a recipe for easy namespacing of the whole framework within the
> Prototype object.

[snip]

> So, what isn't namespaced?

"the whole framework" vs. "what isn't namespaced". Something is wrong here.

> That's it. Happy namespacing,

I fail to see the large benefit gained by partially namespacing
Prototype. If Prototype's fundamental design is usable in a particular
sitation then a namespaced Prototype is not necessary. And if a
partially namespaced Prototype is supposed to make Prototype usable in
a situation where it otherwise isn't then the namespacing of "Toggle"
probably won't save the higher likelyhood of a clash in other code's
augmentation of the Function prototype, for example.

Peter
-------
http://forkjavascript.org

Mislav

unread,
Dec 3, 2006, 5:58:18 AM12/3/06
to Ruby on Rails: Spinoffs
On Dec 3, 7:46 am, "Peter Michaux" <petermich...@gmail.com> wrote:
> "the whole framework" vs. "what isn't namespaced". Something is wrong here.

I've removed the framework from the global namespace (the window
object) - if that isn't namespacing, I don't know what is.

Yes, prototype extensions (points 3 and 4) can't be namespaced. They
CAN be extracted to separate objects within the Prototype namespace but
that would be just plain silly. Exactly *how likely* you think these
conflicts with other libraries are?

-M

Peter Michaux

unread,
Dec 3, 2006, 11:20:24 AM12/3/06
to rubyonrail...@googlegroups.com
Hi Mislav,

On 12/3/06, Mislav <mislav....@gmail.com> wrote:
>
> On Dec 3, 7:46 am, "Peter Michaux" <petermich...@gmail.com> wrote:
> > "the whole framework" vs. "what isn't namespaced". Something is wrong here.
>
> I've removed the framework from the global namespace (the window
> object) - if that isn't namespacing, I don't know what is.

The framework may not be in the global space but it is still in
multiple, common, shared spaces. Removing the framework from the
window object is not enough. To call this namespaced would mislead
people.


> Yes, prototype extensions (points 3 and 4) can't be namespaced.

So you haven't namespaced the entire library. There seems to be a
contradiction in your statements: You've namedspaced the whole library
except certain things. This statement can never be true.


> They
> CAN be extracted to separate objects within the Prototype namespace but
> that would be just plain silly.

Yes. Pointless. It would destroy the idea of Prototype.


> Exactly *how likely* you think these
> conflicts with other libraries are?

Conflicts are a game of chance and percentages. I think it is much
more likely that another piece of JavaScript has String.gsub() or
Array.first() then Toggle.display().

Peter

Reply all
Reply to author
Forward
0 new messages