天天看點

Array.prototype.slice.call

在研究某個架構源碼的時候,看到的。

查了下資料,

1.兩個部分,一個是String.slice()

slice( ) returns a string containing a slice, or substring, of string. It does not modify string。

slice()傳回一個子片段,對原先的string沒有影響,還可以用負數當參數。

Example:

[javascript]  view plain copy

  1. //from javascript-the definitive Guide 5th Edition  
  2. var s = "abcdefg";  
  3. s.slice(0,4)    // Returns "abcd"  
  4. s.slice(2,4)    // Returns "cd"  
  5. s.slice(4)      // Returns "efg"  
  6. s.slice(3,-1)   // Returns "def"  
  7. s.slice(3,-2)   // Returns "de"  
  8. 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 plain copy

  1. var a = [1,2,3,4,5];  
  2. a.slice(0,3);    // Returns [1,2,3]  
  3. a.slice(3);      // Returns [4,5]  
  4. a.slice(1,-1);   // Returns [2,3,4]  
  5. a.slice(-3,-2);  // Returns [3]; buggy in IE 4: returns [1,2,3]  

除了正常用法,slice 經常用來将 array-like 對象轉換為 true array。在一些架構中會經常有這種用法。

[javascript]  view plain copy

  1. Array.prototype.slice.call(arguments,0);//将參數轉換成真正的數組  

call的作用是改變this的指向,就相當于arguments調用了,slice這個方法。0就是start=0,end沒指定,是以傳回整個arguments,這個時候就轉換成數組了。

這裡有一個問題,

[javascript]  view plain copy

  1. 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的意思了。

繼續閱讀