As to when querySelectorAll () got it, it's not DOMlist?.

I see that it is written in a plug-in: determine whether it is a function of DOMlist and encapsulate document.querySelectorAll , as follows

//  DOM List
function isDOMList(selector) {
    if (!selector) {
        return false;
    }
    if (selector instanceof HTMLCollection || selector instanceof NodeList) {
        return true;
    }
    return false;
}

//  document.querySelectorAll
function querySelectorAll(selector) {
    var result = document.querySelectorAll(selector);
    if (isDOMList(result)) {
        return result;
    } else {
        return [result];
    }
}

then I wondered why I re-encapsulated querySelectorAll () , and then I wrote a Demo and found that no matter what, querySelectorAll () always got DOMlist . I would like to ask whether this encapsulation determines the role of DOMlist , or under what circumstances, my demo will return false

.

here is the demo I wrote

<body>
<ul>
  <li class="nobe">a</li>
  <li>b</li>
  <li>c</li>
  <li>d</li>
  <li class="nobe">e</li>
</ul>
<button onclick="checkDom()">li</button>
<script type="text/javascript">
  function isDOMList(selector) {
    if (!selector) {
      return false;
    }
    if (selector instanceof HTMLCollection || selector instanceof NodeList) {
      return true;
    }
    return false;
  }

  function qSA(selector) {
    var result = document.querySelectorAll(selector);
    if (isDOMList(result)) {
      console.log("DOMlist");
      return result;
    } else {
      console.log("DOMlist");
      return [result];
    }
  }

  function checkDom() {
    console.log(qSA(".nobe"));
  }
</script>
</body>
Mar.25,2021

dom-core.js-sharpL25

the landlord only sends this code. Of course it's useless.

  1. first of all, why does this plug-in package querySelectorAll ? it encapsulates selectors of API into selector .
  2. the second reason is isDOMList , because other people's plug-in find methods expose querySelectorAll , and other people's selector types can also be said to be Nodelist , which can be nested.
  3. and then isDOMList Why should you write this, because this method is not just used here.

so what's the use of bringing up this piece of code alone, of course it's useless.


which plug-in, send it to see?

querySelectorAll on

MDN explicitly returns Nodelist ..

Menu