How To Subclass The JavaScript Array Object

Dean Edwards written about the hack that allows to subclass the Arrary Object in JavaScript. Here is what he says —”Most library authors would love to extend the Array object (especially for those tasty iteration methods) but shy away from doing so for fear of breaking other scripts. So nearly all (with the noteable exception […]

Dean Edwards written about the hack that allows to subclass the Arrary Object in JavaScript. Here is what he says —”Most library authors would love to extend the Array object (especially for those tasty iteration methods) but shy away from doing so for fear of breaking other scripts. So nearly all (with the noteable exception of Prototype) leave Array and other built-in objects alone.

But I’ve been playing with iframes

Every JavaScript environment (i.e. a browser window or iframe) has its own copy of the core JavaScript objects and classes. What if we “borrow” one of those objects?”

Hedger Wang also has a new hack that uses window.createPopup instead of an iframe to for IE to fix some issues. Take a look at the code below:
;(function(){ if(!window.createPopup){return}; var fs = function(){ /==/ var Array2 = parent.Array2 ; var p1 = Array.prototype ; var p2 = Array2.prototype; for(i in p2 ){ p1[i] = p2[i]; }; parent.Array2 = Array; parent.document.title = 'Array2 is ready';/*debug msg*/ /==/ }; document.title = 'Prepare Array2';/*debug msg*/fs = (fs + '').split('/==/')[1]; window.createPopup().document.body.innerHTML = '<img src="null" onerror="' + fs + '" / />'; })();var Array2 = function(){}; Array2.prototype = new Array; Array2.prototype.bar = function(){ alert("this.bar() --> This.Length=" + this.length ); }

Dean Edwards | ajaxian

How To, Subclass, JavaScript, Array Object