I have noticed that one of my friends uses [].slice.call()
in order to populate an array with matched elements. This technique is different from what I am familiar with, which is using Array.prototype.push
for this purpose.
This got me curious about how exactly Array.prototype.slice
helps fill an array and what roles it plays compared to Array.prototype.push
when working with objects and arrays.
Here are the specific questions I have:
- What is the mechanism behind using
Array.prototype.slice
to populate an array? - How do
Array.prototype.slice
andArray.prototype.push
differ in their functions related to objects and arrays?
var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)