Why does document.querySelectorAll return a StaticNodeList rather than a real Array?

前端 未结 6 819
北恋
北恋 2020-11-30 01:11

It bugs me that I can\'t just do document.querySelectorAll(...).map(...) even in Firefox 3.6, and I still can\'t find an answer, so I thought I\'d cross-post on

6条回答
  •  误落风尘
    2020-11-30 01:45

    This is an option I wanted to add to the range of other possibilities suggested by others here. It's meant for intellectual fun only and is not advised.


    Just for the fun of it, here's a way to "force" querySelectorAll to kneel down and bow to you:

    Element.prototype.querySelectorAll = (function(QSA){
        return function(){
            return [...QSA.call(this, arguments[0])]
        }
    })(Element.prototype.querySelectorAll);
    

    Now it feels good to step all over that function, showing it who's the boss. Now I don't know what's better, creating a whole new named function wrapper and then have all your code use that weird name (pretty much jQuery-style) or override the function like above once so the rest of your code would still be able to use the original DOM method name querySelectorAll.

    • Such approach would eliminate possible use of sub-methods

    I wouldn't recommend this in any way, unless you honestly don't give a [you know what].

提交回复
热议问题