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 |