Friday, November 24th, 2006

Another trick to allow Array subclasses

Category: JavaScript, Tip

<p>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:

javascript
< view plain text >
  1. ;(function(){
  2.    
  3.     if(!window.createPopup){return};
  4.    
  5.     var fs = function(){
  6.    
  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.     };
  18.    
  19.     document.title = 'Prepare Array2';/*debug msg*/
  20.  
  21.     fs = (fs + '').split('/==/')[1];
  22.     window.createPopup().document.body.innerHTML = '<img src="null" onerror="' +  fs + '" />';
  23.    
  24. })();
  25.  
  26. var Array2 = function(){};
  27.     Array2.prototype = new Array;
  28.     Array2.prototype.bar = function(){
  29.     alert("this.bar() --> This.Length=" + this.length );
  30.     }

Related Content:

10 Comments »

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

一ヶ月で18人の女性をイカせた【究極のオーガズムテクニック】現役風俗嬢3人が暴露!!

今まで一人もイカせたことのなかった男性が、一ヶ月で18人の女性をイカせることが出来た究極のオーガズムテクニックとは?全ての女性が満足するだけじゃなく、やみつきになってしまã…

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

test

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 : http://ajaxcode.blogspot.com

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: http://alex.dojotoolkit.org/?p=538

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.