Created
July 23, 2014 07:59
-
-
Save CooLNuanfeng/fdf2c09f9e6ce17f6877 to your computer and use it in GitHub Desktop.
将伪数组转化为数组
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; | |
} | |
} |
zhoufenfens
commented
Oct 17, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment