将函数的实际参数转换成数组
2012-11-05 16:36
344 查看
转自:http://www.planabc.net/2010/01/06/arguments_to_array/
实际参数在函数中我们可以使用
(注:形参可通过
值得庆幸的是,我们可以通过数组的
对于
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, 0);// 怿飞注:下称方法一
do this…
var args = [].slice.call(arguments, 0); // 怿飞注:下称方法二
但二者的性能差异真的存在吗?经过个人简单测试发现:
在
2010年1月30日更新(测试地址):几经验证,性能差异不大,反而第一张方法性能稍优势一点,或许由于第二种方法创建新数组产生开销。
最后附上方法三,最老土的方式:
不过对于平常来说,个人建议使用第一种方法,但任何解决方案,没有最好的,只有最合适:
------------------------------------------------------------------
如何将 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.
实际参数在函数中我们可以使用
arguments对象获得
(注:形参可通过
arguments.callee获得),虽然
arguments对象与数组形似,但仍不是真正意义上的数组。
值得庆幸的是,我们可以通过数组的
slice方法将
arguments对象转换成真正的数组:
var args = Array.prototype.slice.call(arguments, 0);
对于
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, 0);// 怿飞注:下称方法一
do this…
var args = [].slice.call(arguments, 0); // 怿飞注:下称方法二
但二者的性能差异真的存在吗?经过个人简单测试发现:
在
arguments.length较小的时候,方法二性能上稍有一点点优势,而在
arguments.length较大的时候,方法一却又稍有优势。
2010年1月30日更新(测试地址):几经验证,性能差异不大,反而第一张方法性能稍优势一点,或许由于第二种方法创建新数组产生开销。
最后附上方法三,最老土的方式:
var args = []; for (var i = 1; i < arguments.length; i++) { args.push(arguments[i]); }
不过对于平常来说,个人建议使用第一种方法,但任何解决方案,没有最好的,只有最合适:
var args = Array.prototype.slice.call(arguments, 0);
------------------------------------------------------------------
如何将 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.
相关文章推荐
- 如何将函数的实际参数转换成数组
- 将函数的实际参数转换成数组的方法
- JS基础篇--函数实际参数转换成数组的方法[].slice.call(arguments)
- 将函数的实际参数转换成数组的方法
- 将函数的实际参数转换成数组的方法,习惯用Array.prototype.slice.call(arguments)
- 将函数的实际参数转换成数组的方法
- 将函数的实际参数转换成数组的方法
- JS函数arguments数组获得实际传参数个数的实现方法
- 如何将数组转换为多个参数赋值给另一个函数或方法
- 数组作为函数参数 自动转换为同类型指针
- JS函数arguments数组获得实际传参数个数
- B继承自A,A指针无法隐式转换为B指针,函数参数只管指针类型,与实际指向对象无关
- 【笔记】js Array.prototype.slice.call(arguments) 将函数的参数转换为数组方法的见解
- 把函数参数转换到一个数组中
- c 数组和指针在函数参数中的转换
- http_build_query()就是将一个数组转换成url 问号?后面的参数字符串,并且会自动进行urlencode处理,及它的逆向函数
- B继承自A,A指针无法隐式转换为B指针,函数参数只管指针类型,与实际指向对象无关
- 接收数组的函数模板对参数类型推断
- C#调用VC的DLL的接口函数参数类型转换
- jquery如何把参数列严格转换成数组实现思路