将"类数组对象"转换成数组对象

javascript与dom有许多瑕疵,如著名的类数组对象Arguments,其他诸如HTMLCollection,NodeList如果它们都是数组的子类,那多省时啊。在标准浏览器中,好像只要对象存在length属性,就能把它转换为数组,但IE就不尽然。

//※※※※※※※※※※※测试1※※※※※※※※※※※※※※
function  test(){
   alert(arguments)
   arguments = Array.prototype.slice.call(arguments);
   alert(arguments instanceof  Array);
   alert(arguments)
}
test(1,2,3,4);
//※※※※※※※※※※※测试2※※※※※※※※※※※※※※
var  htmlcollection = document.getElementsByTagName( "h3" );
alert(htmlcollection)
try {
   htmlcollection = Array.prototype.slice.call(htmlcollection);
   alert(htmlcollection instanceof  Array);
   alert(htmlcollection);
} catch (e){
   alert(e)
}
//※※※※※※※※※※※测试3※※※※※※※※※※※※※※
var  sheets = document.styleSheets;
alert(sheets)
try {
   sheets = Array.prototype.slice.call(sheets);
   alert(sheets instanceof  Array);
   alert(sheets);
} catch (e){
   alert(e)
}
//※※※※※※※※※※※测试4※※※※※※※※※※※※※※
var  arrayLike = {
   0 : '司徒正美' ,
   1 : '司徒正美' ,
   2 : '司徒正美' ,
   length : 3
};
alert(arrayLike)
arrayLike = Array.prototype.slice.call(arrayLike);
alert(arrayLike instanceof  Array);
alert(arrayLike);

原帖:http://www.cnblogs.com/rubylouvre/archive/2009/11/30/1614087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值