querySelectorAll
is a method on DOM elements that accepts a CSS selector and returns a static NodeList
of matching elements.
Array.prototype.slice.call
is one way to turn that NodeList
(which acts like an array, but doesn’t have the methods from Array.prototype
) into a real array.
Give it a try on this page in your browser’s console!
> var headers = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
undefined
> headers.map(function(el) { return el.textContent; })
TypeError: Object #<NodeList> has no method 'map'
> headers = Array.prototype.slice.call(headers);
…
> headers.map(function(el) { return el.textContent; })
["What does Array.prototype.slice.call() & wrapper.querySelectorAll() do?", …]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…