arrays - Explanation of [].slice.call in javascript? -
I stumbled upon this clean shortcut to change a DOM nodelist in a regular array, but I have to accept, I do not fully understand how it works:
[] Slice.call (document.querySelectorAll ('a'), 0)
then it starts with an empty array []
, then slice
is used to change the result of call
for a new array?
Little does I understand that how does call
change from document.querySelectorAll ('a')
to a regular array?
What's happening here that you call slice ()
As it was the function of nodilist
was the function of call ()
. In this case, what is the slice ()
, an empty array is created, then the object that is running on (basically an array, now a nodilist
) Repeat from and keep adding elements to the empty array of that object, which is finally returned, it is one.
Edit:
Then it starts with an empty array [], then the experiment to change the result of the slice Is a new array Yes?
This is not correct []. The function object returns a function object that contains a function
call ()
which is the first parameter of call ()
to this
The function specifying the call; In other words, the function thinks that this parameter is called from an array instead of ( NodeList
by document.querySelectorAll ('a')
).
Comments
Post a Comment