js--Array.prototype.slice.apply(arguments);
来源:互联网 发布:mac上的解压缩软件 编辑:程序博客网 时间:2024/06/07 11:13
arguments在JavaScript语法中是函数特有的一个对象属性(Arguments对象),用来引用调用该函数时传递的实际参数。
function test(){
//将参数转为一个数组var args = Array.prototype.slice.apply(arguments);
alert(args);
}
arguments在JavaScript语法中是函数特有的一个对象属性(Arguments对象),用来引用调用该函数时传递的实际参数。这个对象很象数组,拥有length属性且使用下标的形式来获取其元素,但它又并非真正的Array对象。更多关于Arguments对象的资料请参阅《JavaScript权威指南》。
所以,直接调用arguments.slice()将返回一个"Object doesn't support this property or method"错误,因为arguments不是一个真正的数组。而以上代码调用Array.prototype.slice.apply(arguments)的意义就在于它能将函数的参数对象转化为一个真正的数组。JavaScript脚本引擎如何实现我们不得而知,但这种方式确实是有效的,而且在主流浏览器上都测试通过。另一方面也可推知Arguments对象和Array对象的亲缘关系。如果你在编写JavaScript的时候,常常碰到需要将arguments对象转成Array来处理的情形,这个技巧可以帮上忙。
- js--Array.prototype.slice.apply(arguments);
- Array.prototype.slice.apply(arguments, [1])
- 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- ubuntu12.10 建立qt嵌入式开发环境(一)
- 当工程中资源文件很多时,如何快速地定位到资源文件
- OSG学习笔记02:编程时注意版本的区别
- 使用C++实现JNI接口需要注意的事项
- Offcie 安装出现1706的错误
- js--Array.prototype.slice.apply(arguments);
- rspec 测试
- ODS与DW的区别
- 如何由已知频率分频产生任意频率
- C# Webserivce配置注意事项
- 23 种设计模式
- Java 输入输出流教程(一)(原作:Java IO Tutorial by Jakob Jenkov)
- 50个jQuery代码段帮你成为更出色的JS开发者
- 导入svn项目时eclipse崩溃