How does that convert document.querySelectorAll('a')
from a
NodeList
to a regular array?
This is the code that we have,
[].slice.call(document.querySelectorAll('a'), 0)
Lets dismantle it first,
[] // Array object
.slice // Accessing the function 'slice' present in the prototype of Array
.call // Accessing the function 'call' present in the prototype of function object(slice)
(document.querySelectorAll('a'),0)
// 'call' can have arguments like, (thisArg, arg1,arg2...n).
// So here we are passing the 'thisArg' as an array like object,
// that is a 'nodeList'. It will be served as 'this' object inside of slice function.
// And finally setting 'start' argument of slice as '0' and leaving the 'end'
// argument as 'undefined'
Step: 1 Execution of call
function
- Inside
call
, other than the thisArg
, the rest of the arguments
will be appended to an argument list.
- Now the function
slice
will be invoked by binding its this
value as
thisArg
(array like object came from document.querySelector
) and with the argument list. i.e] argument start
which contains 0
Step: 2 Execution of slice
function invoked inside of call
P.S For better understanding of our scenario some steps that are necessary for our context has been ignored from the original algorithm of call and slice.