Array.prototype.slice.call
2014-11-06 10:12
162 查看
在研究某个框架源码的时候,看到的。
查了下资料,
1.两个部分,一个是String.slice()
slice( ) returns a string containing a slice, or substring, of string. It does not modify string。
slice()返回一个子片段,对原先的string没有影响,还可以用负数当参数。
Example:
[javascript] view
plaincopy
//from javascript-the definitive Guide 5th Edition
var s = "abcdefg";
s.slice(0,4) // Returns "abcd"
s.slice(2,4) // Returns "cd"
s.slice(4) // Returns "efg"
s.slice(3,-1) // Returns "def"
s.slice(3,-2) // Returns "de"
s.slice(-3,-1) // Should return "ef"; returns "abcdef" in IE 4
2.Array.slice(start,end)
slice( ) returns a slice, or subarray, of array. The returned array contains the element specified by start and all subsequent elements up to, but not including, the element specified by end. If end is not specified, the returned array contains all elements
from the start to the end of array.
返回从start开始到end的子数组,如果end这个参数没有被设置,则返回从start开始到最后的数组元素。
Example:
[javascript] view
plaincopy
var a = [1,2,3,4,5];
a.slice(0,3); // Returns [1,2,3]
a.slice(3); // Returns [4,5]
a.slice(1,-1); // Returns [2,3,4]
a.slice(-3,-2); // Returns [3]; buggy in IE 4: returns [1,2,3]
除了正常用法,slice 经常用来将 array-like 对象转换为 true array。在一些框架中会经常有这种用法。
[javascript] view
plaincopy
Array.prototype.slice.call(arguments,0);//将参数转换成真正的数组
call的作用是改变this的指向,就相当于arguments调用了,slice这个方法。0就是start=0,end没指定,所以返回整个arguments,这个时候就转换成数组了。
这里有一个问题,
[javascript] view
plaincopy
arguments.slice(0)//为什么不直接这样呢,非要用call改下this的指向就可以了?见下文的分析
==================华丽丽地分割线==============
玉伯的分析
http://lifesinger.org/blog/2010/05/array-prototype-slice/
读后感: 能用slice方法的,只要有length属性就行。虽然arguments有length属性,但是没有slice方法,所以呢,Array.prototype.slice()执行的时候,Array.prototype已经被call改成arguments了,因为满足slice执行的条件(有length属性),所以没有报错。感觉有点hack的意思了。
查了下资料,
1.两个部分,一个是String.slice()
slice( ) returns a string containing a slice, or substring, of string. It does not modify string。
slice()返回一个子片段,对原先的string没有影响,还可以用负数当参数。
Example:
[javascript] view
plaincopy
//from javascript-the definitive Guide 5th Edition
var s = "abcdefg";
s.slice(0,4) // Returns "abcd"
s.slice(2,4) // Returns "cd"
s.slice(4) // Returns "efg"
s.slice(3,-1) // Returns "def"
s.slice(3,-2) // Returns "de"
s.slice(-3,-1) // Should return "ef"; returns "abcdef" in IE 4
2.Array.slice(start,end)
slice( ) returns a slice, or subarray, of array. The returned array contains the element specified by start and all subsequent elements up to, but not including, the element specified by end. If end is not specified, the returned array contains all elements
from the start to the end of array.
返回从start开始到end的子数组,如果end这个参数没有被设置,则返回从start开始到最后的数组元素。
Example:
[javascript] view
plaincopy
var a = [1,2,3,4,5];
a.slice(0,3); // Returns [1,2,3]
a.slice(3); // Returns [4,5]
a.slice(1,-1); // Returns [2,3,4]
a.slice(-3,-2); // Returns [3]; buggy in IE 4: returns [1,2,3]
除了正常用法,slice 经常用来将 array-like 对象转换为 true array。在一些框架中会经常有这种用法。
[javascript] view
plaincopy
Array.prototype.slice.call(arguments,0);//将参数转换成真正的数组
call的作用是改变this的指向,就相当于arguments调用了,slice这个方法。0就是start=0,end没指定,所以返回整个arguments,这个时候就转换成数组了。
这里有一个问题,
[javascript] view
plaincopy
arguments.slice(0)//为什么不直接这样呢,非要用call改下this的指向就可以了?见下文的分析
==================华丽丽地分割线==============
玉伯的分析
http://lifesinger.org/blog/2010/05/array-prototype-slice/
读后感: 能用slice方法的,只要有length属性就行。虽然arguments有length属性,但是没有slice方法,所以呢,Array.prototype.slice()执行的时候,Array.prototype已经被call改成arguments了,因为满足slice执行的条件(有length属性),所以没有报错。感觉有点hack的意思了。
相关文章推荐
- 浅谈javascript的Array.prototype.slice.call
- Array.prototype.slice.call
- [Javascript] Array.prototype.slice.call(obj)用法
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments,1)原理
- Array.prototype.slice.call()方法详解
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call
- Array.prototype.slice.call(arguments)
- 将函数的实际参数转换成数组的方法,习惯用Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)
- how does Array.prototype.slice.call() work?
- Array.prototype.slice.call(arguments)
- 【笔记】js Array.prototype.slice.call(arguments) 将函数的参数转换为数组方法的见解
- arguments和Array.prototype.slice.call(arguments,0);
- Array.prototype.slice.call(arguments)
- Array.prototype.slice.call(arguments)简析