How to detect HTMLCollection/NodeList in JavaScript?

前端 未结 7 559
刺人心
刺人心 2020-12-05 02:55

I\'m not sure my current implementation is available all the time:

function isNodeList(nodes) {
    var result = Object.prototype.toString.call(nodes);
    /         


        
7条回答
  •  悲&欢浪女
    2020-12-05 03:37

    I created a benchmark of all answers here to see, what is the best approve in speed. Turns out NodeList.prototype.isPrototypeOf(nodes) is by far the fastest. But in a normal use-case nodes instanceof NodeList would be fine too.

    I personally would just not pick the isNodeList function, because its slow, custom and too much overhead.

提交回复
热议问题