One of my friends uses [].slice.call()
to fill an array with matched elements.
I wonder how this works. I just know the Array.prototype.push
to fill an array.
Also I have seen there is a difference in both techniques to fill an array.
So my questions are:
- How does
Array.prototype.slice
help to fill an array? - What are the roles of
Array.prototype.slice
andArray.prototype.push
to make objects and arrays?
Fiddle
var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)
One of my friends uses [].slice.call()
to fill an array with matched elements.
I wonder how this works. I just know the Array.prototype.push
to fill an array.
Also I have seen there is a difference in both techniques to fill an array.
So my questions are:
- How does
Array.prototype.slice
help to fill an array? - What are the roles of
Array.prototype.slice
andArray.prototype.push
to make objects and arrays?
Fiddle
var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)
Share
Improve this question
edited Feb 15, 2014 at 14:54
marionebl
3,40222 silver badges34 bronze badges
asked Feb 15, 2014 at 14:42
JitenderJitender
7,98932 gold badges116 silver badges218 bronze badges
3 Answers
Reset to default 6The .slice()
method of arrays returns a shallow copy of a portion of an array. It's written as a method on the Array prototype, so the array it operates on is the array in whose context it's invoked. Normally that'd be something like this:
var newArray = oldArray.slice(2, 4);
When you call the .slice()
method with .call()
, you can control the value of this
explicitly. In your friend's code, he's passing a jQuery object as the this
value. Because there are no other parameters, .slice()
returns a shallow copy of the jQuery object in its entirety, as a new array. (Like some other similar methods, .slice()
will work on anything that looks like an array: basically, anything that has a .length
property and numerically-indexed properties of interest. A jQuery object fits that description.)
However, since jQuery already has a built-in method to return a plain array of all matched elements, your friend should not bother doing that anymore:
var plainArray = $('[id^=name]').get();
That does exactly the same thing. Using .slice()
isn't wrong, but it's kind-of pointless.
How about $('[id^=name]').get()
?
The [].slice.call
method leverages the array-like length
and numeric key properties of jQuery objects to re-use the intentionally generic methods of Array.prototype
. The method Array.prototype.slice
is documented by the standard to return a new Array
object by performing a specific algorithm that accesses numeric-keyed properties in sequence of the this
object - regardless of its type - assigning the values to sequential elements of the new Array
.
I wouldn't use either approach, because jQuery has get()
, which is documented to return an array of the elements matched. Why use a more obscure or less direct approach?
I does't have any specified reason for this case, but The thing i know is, .slice() also used to create another instance of an array, And here the slice add a space to its instance because it have no parameters.
Javascript is the language where you perform a Single operations in 1000's of different ways ! its depend on your choice! But, We must have to follow the syntax that is made for particular operation.