Array.prototype.slice.call(arguments, 1)
来源:互联网 发布:淘宝买苹果7手机靠谱吗 编辑:程序博客网 时间:2024/05/29 18:14
Array.prototype.slice.call(arguments,1)该语句涉及两个知识点。
arguments是一个关键字,代表当前参数,在javascript中虽然arguments表面上以数组形式来表示,但实际上没有原生数组slice的功能,这里使用call方法算是对arguments对象不完整数组功能的修正。
[].slice(begin,end);
在JS中,[]就是一个数组对象,在执行时,先会建立对象,再执行自身对象下的slice方法,对这个数组进行切割。
而静态调用与OOP方式调用主要目的是静态方式的机器执行效率较高,因为不需要先经过建立对象,而不需要为对象增加内在开销,所以这种写法在较底层的JS程序(如JS框架,兼容性基础库)里面使用得比较多。
Array.prototype.slice就是这样的一个函数,如果要知道为什么不直接写slice而写那么长的三个单词,这不是三言两语说和清楚的,请看JavaScript中的prototype(原型)属性研究 ,如果简单点说,可以用伪代码表示:
Array.prototype.slice=function(begin,end){
var arr=this;//this就是被切割的函数
var newArr=我只要arr数组的begin到end中间的部分;//这才是真正的中文编程
return newArr;//返回被切割后的新数组,强大吧!
}
然后,通过调用函数的call方法:call(thisObj,begin,end),把里面的this强制转换成将要被切割的数组,即术语中的改变函数的scope,从而完成切割。
使用情况:
这个语句通常用于把arguments转换为数组。
function fn() {
a = Array.prototype.slice.call(arguments,0);
alert(a);
}(1,2,3,4,5,6,7,'never-online.net/blog')
该代码片段摘自CSDN WEB开发社区的某帖的2楼。
函数的argements在JS1.5中只是一个类似数组的对象,有length属性,但并不是真正的数组,具体请看:Array-like Objects in JavaScript。(据说在ECMA script2中将会是个真正的数组了)
注意:这个语句可不能随便用哦,有的同学把String作为对象转进去,把abcde变为 [a,b,c,d,e],这可不是万试万灵的哦,不同的浏览器对slice的解释是有所区别的哦。例如Prototype.js中的$A函数也可以把 arguments转为单纯数组,但为了适应更多的情况,它直接使用循环来实现了。
- Array.prototype.slice.call(arguments, 1)
- Array.prototype.slice.call(arguments,1)原理
- Array.prototype.slice.call(arguments,1)原理
- Array.prototype.slice.call(arguments,1)
- 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)
- 那些让 Web 开发者们深感意外的事情
- 进程间通信(IPC)
- 硬件环境
- QT中TCP/UDP通信数据的组织
- 计数排序和基数排序
- Array.prototype.slice.call(arguments, 1)
- GAE SDK 1.4.0 发布了!
- JavaScript: 使用面向对象的技术创建高级 Web 应用程序
- PHP Notice: Use of undefined constant
- Run JMeter by Ant
- 从概念理解Lucene的Index(索引)文档模型
- 完美解码使用帮助
- WPF指南之WPF的结构——出自《IT168》
- Linux下网络问题