In this example, is passed through this?

Good day! I am new to JS.
Here here , the following piece of code:
selectorMatches function(el, selector) {
 var p = Element.prototype;
 var f = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector || function(s) {
 return [].indexOf.call(document.querySelectorAll(s), this) !== -1;
};
 return f.call(el, selector);
}


I understand that .call first passed parameter indicates the context.
That is, in the end
return [].indexOf.call(document.querySelectorAll(s), this) !== -1;
will be
return document.querySelectorAll(s).IndexOf(this) !== -1;


But how this correlates with the very challenge in line with the return f.call(element, selector);?
As the context is transferred to element be used? and isn't that a simple analogue querySelectorAll(selector) get?
June 8th 19 at 17:03
1 answer
June 8th 19 at 17:05
Solution
You got that right, for ideas
[].indexOf.call(document.querySelectorAll(s), this)

what
document.querySelectorAll(s).indexOf(this)

But document.querySelectorAll() returns a collection of type NodeList and is not the same as Array, so it does not have its own indexOf () method. So structure [].indexOf.call(document.querySelectorAll(s), this) - this is a tricky way to do indexOf on an object which has no method indexOf. I suspect that somewhere in the bowels of the indexOf() turns this into an array by using Array.from(). This can be seen, for example, having "1" when execution of [].indexOf.call('abc', 'b') and "-1" when performing [].indexOf.call(1, 1).

Good initial design can be rewritten in

Array.from(document.querySelectorAll(s)).indexOf(this)


Then it will cause less questions.

Find more questions by tags JavaScript