将函数的实际参数转换成数组的方法
2010-01-25 00:00
363 查看
值得庆幸的是,我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组:
var args = Array.prototype.slice.call(arguments);
对于slice 方法,ECMAScript 262 中 15.4.4.10 Array.prototype.slice (start, end) 章节有备注:
《Pro JavaScript Design Patterns》(《JavaScript 设计模式》)的作者 Dustin Diaz 曾指出:
但二者的性能差异真的存在吗?经过个人简单测试发现:
在 arguments.length 较小的时候,方法二性能上稍有一点点优势,而在arguments.length 较大的时候,方法一却又稍有优势。
最后附上方法三,最老土的方式:
不过对于平常来说,个人建议还是使用第二种方法,但任何解决方案,没有最好的,只有最合适:
一般的函数的 arguments.length 都在 10 以内,方法二有优势;
方法二的代码量上也比第一种少,至少可以减小一点字节 ^^
如何将 NodeList (比如:document.getElementsByTagName('div'))转换成数组呢?
解决方案简单如下:
为什么 IE 中 NodeList 不可以使用 [].slice.call(nodes) 方法转换呢?
In Internet Explorer it throws an error that it can't run Array.prototype.slice.call(nodes) because a DOM NodeList is not a JavaScript object.
var args = Array.prototype.slice.call(arguments);
对于slice 方法,ECMAScript 262 中 15.4.4.10 Array.prototype.slice (start, end) 章节有备注:
The slice function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the slice function can be applied successfully to a host object is implementation-dependent.
《Pro JavaScript Design Patterns》(《JavaScript 设计模式》)的作者 Dustin Diaz 曾指出:
instead of… var args = Array.prototype.slice.call(arguments); // 怿飞注:下称方法一 do this… var args = [].slice.call(arguments, 0); // 怿飞注:下称方法二
但二者的性能差异真的存在吗?经过个人简单测试发现:
在 arguments.length 较小的时候,方法二性能上稍有一点点优势,而在arguments.length 较大的时候,方法一却又稍有优势。
最后附上方法三,最老土的方式:
var args = []; for (var i = 1; i < arguments.length; i++) { args.push(arguments[i]); }
不过对于平常来说,个人建议还是使用第二种方法,但任何解决方案,没有最好的,只有最合适:
var args = [].slice.call(arguments, 0); 理由有二:
一般的函数的 arguments.length 都在 10 以内,方法二有优势;
方法二的代码量上也比第一种少,至少可以减小一点字节 ^^
如何将 NodeList (比如:document.getElementsByTagName('div'))转换成数组呢?
解决方案简单如下:
function nodeListToArray(nodes){ var arr, length; try { // works in every browser except IE arr = [].slice.call(nodes); return arr; } catch(err){ // slower, but works in IE arr = []; length = nodes.length; for(var i = 0; i < length; i++){ arr.push(nodes[i]); } return arr; } }
为什么 IE 中 NodeList 不可以使用 [].slice.call(nodes) 方法转换呢?
In Internet Explorer it throws an error that it can't run Array.prototype.slice.call(nodes) because a DOM NodeList is not a JavaScript object.
相关文章推荐
- 将函数的实际参数转换成数组的方法
- 将函数的实际参数转换成数组的方法,习惯用Array.prototype.slice.call(arguments)
- 将函数的实际参数转换成数组的方法
- JS基础篇--函数实际参数转换成数组的方法[].slice.call(arguments)
- 将函数的实际参数转换成数组的方法
- 将函数的实际参数转换成数组
- 如何将函数的实际参数转换成数组
- 【笔记】js Array.prototype.slice.call(arguments) 将函数的参数转换为数组方法的见解
- JS函数arguments数组获得实际传参数个数的实现方法
- 如何将数组转换为多个参数赋值给另一个函数或方法
- QT 自定义信号与信号槽方法及解决自定义数据类型或数组作为函数参数的问题
- 黑马程序员—反射调用main方法的问题总结(涉及可变参数)(反射参数是一个数组的函数要小心)
- 数组作为函数参数并传递数组元素个数的几种有效方法
- PHP对象转换为数组函数(递归方法)
- LINQ 要转换为 SQL,Math.Round 方法需要 MidpointRounding 参数。使用“AwayFromZero”来指定 SQL 函数 ROUND。
- c++中关于数组作为函数参数并传递数组元素个数的几种有效方法的讨论
- 数组原型方法调用及函数apply调用时 类数组参数在IE8下的问题
- thinkPHP传数组到模板页面volist每一列加一个button,点击按钮携带参数传给JS函数,跳转到不同的方法
- C#调用VC DLL接口函数参数类型转换方法介绍
- PHP对象转换为数组函数(递归方法)