Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
30 December 2005 @ 2pm

Tagged
Ajax, Javascript, Rails, WebDev

Discuss

Prototype and extending javascript’s Object or Array

James McParlane wrote a entry detailing why he won’t use the Prototype library, as extending Object or Array’s builtin prototype is bad.

However, Sam heard the complaints about how Prototype 1.3.1 was breaking existing code and other libraries, and the newer relases all use Object.extend() for safe extensions and do not change Array’s prototype. Note that the prototype home page still only lists the old 1.3.1 version, which may be confusing some people. So either download just the .js file, or grab the latest Scriptaculous which has the 1.4 final also, and use the for…in without fear.


7 Comments

Posted by
James Mc Parlane
6 January 2006 @ 10am

Thanks for pointing that out.

I just tried the latest 1.4.0 version against my testcases.

Its still breaks for..in :(

A function is just another object - so it is still iterated.

The file has doubled in size and there is now 33 extra objects instead of one.


Posted by
telepethetic
24 January 2006 @ 7pm

umm… check out lines 403 through 407 in version 1.4.0 of prototype:

403 Object.extend(Array.prototype, Enumerable);
404
405 Array.prototype._reverse = Array.prototype.reverse;
406
407 Object.extend(Array.prototype, {

how exactly does this NOT change Array.prototype???

still breaks for-in loops


Posted by
John
12 February 2006 @ 9pm

TOTALLY AGREE. It still breaking because of the line. even in version 1.4.0
………..grrrrrrrr……

Object.extend = function(destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
}


Posted by
Chetan
11 April 2006 @ 4am

How about instead of mucking with Array object, create a new subclass of Array and add Enumerable to it along with all the other prototype goodies for Array.
Then $A() can return this new “Array on Steriod” with all the new methods. If you need to use the new methods on regular Javascript arrays, simply wrap them up in a $A call which can do a smart clone or something.


Posted by
joe schmoe
21 February 2007 @ 3pm

how about you all just use the javascript language correctly. its not prototype that is wrong its your code. javascript does NOT have associative arrays or hashes.


Posted by
Rob
21 February 2007 @ 3pm

joe: way to completely misread a very old and out of date post.


Posted by
Jordan
30 December 2007 @ 11pm

Although I agree that Object.prototype should be verboten, I’m a little confused by the fuss over extending the Array object. The for..in construct is less efficient, and is not intended to be used to iterate over elements of an Array object. What’s wrong with using a for loop?


Leave a Comment

The problem with JavaSchools according to Joel Ajaxian redesigned (and finally on Wordpress)