Skip to content

Instantly share code, notes, and snippets.

@CooLNuanfeng
Created July 23, 2014 07:59
Show Gist options
  • Save CooLNuanfeng/fdf2c09f9e6ce17f6877 to your computer and use it in GitHub Desktop.
Save CooLNuanfeng/fdf2c09f9e6ce17f6877 to your computer and use it in GitHub Desktop.
将伪数组转化为数组
/**
* 这里把符合以下条件的对象称为伪数组
* 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
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment