Friday, November 24th, 2006
Another trick to allow Array subclasses
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:
-
-
;(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 );
-
}
-












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.
summoning the aforementioned spelling cow…
一ヶ月ã§18人ã®å¥³æ€§ã‚’イカã›ãŸã€ç©¶æ¥µã®ã‚ªãƒ¼ã‚¬ã‚ºãƒ テクニック】ç¾å½¹é¢¨ä¿—嬢3äººãŒæš´éœ²ï¼ï¼
今ã¾ã§ä¸€äººã‚‚イカã›ãŸã“ã¨ã®ãªã‹ã£ãŸç”·æ€§ãŒã€ä¸€ãƒ¶æœˆã§18人ã®å¥³æ€§ã‚’イカã›ã‚‹ã“ã¨ãŒå‡ºæ¥ãŸç©¶æ¥µã®ã‚ªãƒ¼ã‚¬ã‚ºãƒ テクニックã¨ã¯ï¼Ÿå…¨ã¦ã®å¥³æ€§ãŒæº€è¶³ã™ã‚‹ã ã‘ã˜ã‚ƒãªãã€ã‚„ã¿ã¤ãã«ãªã£ã¦ã—ã¾ã…
test
sdt sdfhg sdfh sdfh sdfhsd
@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.
That is pretty much the point of the article.
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”.
See also : http://ajaxcode.blogspot.com
@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. :-)
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
sister that will suck our dicks
bondage fetish shemales nude ring gag
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.