MacIE5でArray.pushを実装
MacIE5でArray.pushが必要になったんで、作りました。以下のコードをJavaScriptのコードに入れておくと、Arrayオブジェクトにpushメソッドが追加されます。pushメソッドが既にある場合は、上書きしません。
// Array.pushをMacIE5に実装
if (typeof Array.prototype.push != 'function') {
Array.prototype.push = function (f){
this[this.length] = f;
}
}
使うときは普通に
var x = ['hoge', 'fuga'];
x.push('hage');
alert(x.length); // 3
var s = '';
for (var i = 0; i < x.length; i++ ) {
s += i + ':' + x[i] + ',';
}
alert(s); // 0:hoge,1:fuga,2:hage,
となります。
作ってから検索してみたら、先人が同じコードを作ってた。よくあることだねー
IE5.5以前ではArray.push()が使えないらしいので - へぼいいいわけ [d.hatena.ne.jp]
あと、同様に昔作ったMacIE5でdecodeURIを実装もご覧ください。
トラックバック
この記事のトラックバックURL:
http://app.cocolog-nifty.com/t/trackback/19018/42347612
この記事へのトラックバック一覧です: MacIE5でArray.pushを実装:
コメント
Array.prototype.push = function(){
for(var i=0; i
this[this.length]=arguments[i]
}
return this.length;
}
とかじゃないとpushの仕様を満たさないかも?
投稿者: otsune (2008/09/02 14:18:23)
Array.prototype.push = function(){
for(var i=0; i < arguments.length; i++){
this[this.length]=arguments[i]
}
return this.length;
}
とかじゃないとpushの仕様を満たさないかも?
投稿者: otsune (2008/09/02 14:19:30)






![森田 創: WEB+DB PRESS 総集編 [Vol.1~60]](http://ecx.images-amazon.com/images/I/51aE58LxWlL._SL75_.jpg)
















![: 電子ルーペ ViewPe[ビューペ] 3R-MSV35 マイクロスコープ デジタル顕微鏡 高機能 低価格 読書 植物 研究 肌チェック](http://ecx.images-amazon.com/images/I/41erkiQgAsL._SL75_.jpg)





![: Optimus bright [docomo L-07C] 対応 予備バッテリー 1500mAh BL-44JN](http://ecx.images-amazon.com/images/I/41gjqsDjq%2BL._SL75_.jpg)










![: パンツの穴 [DVD]](http://ecx.images-amazon.com/images/I/21R9SEY1HPL._SL75_.jpg)

















