views:

120

answers:

3

I just spent almost 2 hours debugging a third party JavaScript library only to find that an array which I pass to that library gets converted to a string somewhere down the pipe... I don't know why or how that happens, but as soon as I remove Prototype from my project, everything works fine again.

Could that be because of Prototype extending the DOM? What's my best option? I use Prototype's element iterators, DOM manipulation, bind() method, and string manipulators in my project, and I'd rather not lose them.

Is there a library which has all that, but which works fine with 3rd party JS libraries that are sensitive to DOM extensions?

A: 

I have had success with jQuery. If you call jQuery.noConflict(), it will work very well with other JS libraries.

Rob Di Marco
Thanks for the pointer, however, the problem is not that the $ variable collides with other libraries. It's the object extensions Prototype applies which caused the trouble.
Matthias
No, I get that. JQuery is nice in that it contains much of the same functionality but without the object extensions that can cause trouble with prototype and mootools
Rob Di Marco
+2  A: 

Having worked with Prototype before in the past, there is no way to have it "play nice" (akin to jQuery's noConflict mode).

The very approach behind Prototype prevents this. Prototype's bread & butter comes from its Object.extend() method that gets called on objects as soon as you manipulate them with Prototype methods. Not to mention Prototype also has already modified core JavaScript objects before messing with yours.

On the other hand, jQuery is all self-contained in the jQuery object (which is, to my knowledge, all it adds to the JavaScript namespace when included).

Are there other libraries that have similar features? Sure, in fact, and I'm sure you're sick of hearing this by now, but you could probably do most of it with jQuery. However, you won't avoid the unavoidable: re-writing code. It won't be done the same way with any other framework, let alone give you the same results/beahvior.

Your choices are thus:

  • Drop Prototype and re-write stuff that did use it to use something else
  • Drop the third party library in favor of alternative or absence entirely
  • Re-write prototype.js or thirdpartytool.js to play nicely (this, of course, is a terrible hack that will cause more headaches when you want to "upgrade" one or both tools in the future when a new release comes out)
Yadyn
I should note that, having used both Prototype and jQuery, I like both and each have their strengths. I'm often missing abilities from each when project x I'm working on can only use one of them. But that's just it: I've purposefully avoided mixing the two.
Yadyn
Thanks for you answer. I now use jQuery and the dot-string extension (because I heavily rely on string normalization).I am still missing some things like a way of binding 'this', but most of the things I need are there.
Matthias
A: