Last active
August 29, 2015 14:03
-
-
Save hehongwei44/d0e8388540883f43f142 to your computer and use it in GitHub Desktop.
将arguments(参数数组)和nodeList(元素节点转换成元素集合)转换成数组
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 这里把符合以下条件的对象称为伪数组 | |
* 1,具有length属性 | |
* 2,按索引方式存储数据 | |
* 3,不具有数组的push,pop等方法 | |
* | |
*/ | |
//将伪数组转换成数组,ps:已测试IE6-10、chrome、Firefox | |
function toArray(arg){ | |
try { | |
//转换arguments | |
return Array.prototype.slice.call(arg); | |
}catch(e){ | |
//转换元素节点元素 | |
var arr = []; | |
for(var i = 0; i < arg.length; i++){ | |
arr[i] = arg[i]; | |
} | |
return arr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment