Friday, November 24th, 2006

Another trick to allow Array subclasses

Category: JavaScript, Tip

Dean Edwards talked about his hack that got us the ability to subclass the Array object in JavaScript.

Hedger Wang has a new hack that uses window.createPopup instead of an iframe to for IE to fix some issues.

If you take a view source to see what is in there, you get the gem:


  1. ;(function(){
  3.     if(!window.createPopup){return};
  5.     var fs = function(){
  7.         /==/
  8.         var Array2 = parent.Array2 ;
  9.         var p1 = Array.prototype ;
  10.         var p2 = Array2.prototype;
  11.         for(i in p2 ){
  12.             p1[i] = p2[i];
  13.         };
  14.         parent.Array2 = Array;
  15.         parent.document.title = 'Array2 is ready';/*debug msg*/
  16.         /==/
  17.     };
  19.     document.title = 'Prepare Array2';/*debug msg*/
  21.     fs = (fs + '').split('/==/')[1];
  22.     window.createPopup().document.body.innerHTML = '<img src="null" onerror="' +  fs + '" />';
  24. })();
  26. var Array2 = function(){};
  27.     Array2.prototype = new Array;
  28. = function(){
  29.     alert(" --> This.Length=" + this.length );
  30.     }

Posted by Dion Almaer at 9:59 am

3 rating from 21 votes


Comments feed TrackBack URI

Create a pop-up to override the behaviour of array? We could just not override array. There is no instance where overriding array is that import or would ever be worth this hack. Create your own damn class if you need to break array that much.

Comment by Dan — November 24, 2006

Hedger Wang has a new hack that uses window.createPopup instead of an iframe to for IE to fix some issues.

summoning the aforementioned spelling cow…

Comment by Chris Heilmann — November 24, 2006



Trackback by 情報商材の喫茶店 — November 24, 2006


sdt sdfhg sdfh sdfh sdfhsd

Trackback by lhlkhjkl — November 24, 2006

@Dan – the Prototype library is used by thousands of sites. It extends the Array object so I would hardly say this is not important. Subclassing the JavaScript is very difficult because IE does not maintain the length property.

Create your own damn class if you need to break array that much.

That is pretty much the point of the article.

Comment by Dean Edwards — November 24, 2006

The fact that prototype mangles the built-in types doesn’t mean one should. Providing multiple examples of bad practices doesn’t magically make them good practices. If I can find enough people that drink-and-drive we’ll try to get the laws changed, because now it’s okay – there’s a bunch of us doing it. As for the point of the article, subclassing an array via a hack involving a new window is not creating your own class. Create a vector class. Efficiency obviously is not a concern at the point you would use the above code. It’s sad if you don’t see the difference between the two approaches. The actual point of the article is “just because you can doesn’t mean you should”.

Comment by Dan — November 24, 2006

See also :

Comment by best — November 24, 2006

@Dan – “because you can doesn’t mean you should”. Whatever. But if you want to then this how to do it.

I think you’ve missed the point slightly as this article is about how not to mangle prototypes. Everybody wins. :-)

Comment by Dean Edwards — November 25, 2006

you can also use new ActiveXObject(”htmlfile”); in IE instead of an iframe (I know the popup method has been discuss for IE too). A link discussing the ActiveXObject(”htmlfile”) can be found here:

Comment by Mario — November 30, 2006

This hack has been used in Dojo 0.9 but it eats up a lot of ressources when the javascript is loading. Memory usage increases with each refresh and never reaches a steady state.

Comment by Panya — September 7, 2007

Leave a comment

You must be logged in to post a comment.