-1

Can someone explain to me the following lines from this repository:

    defineProperties(Element.prototype, {
        querySelector: {
            value: querySelectorPatched,
            writable: true,
            enumerable: true,
            configurable: true,
        },
        querySelectorAll: {
            value(this: HTMLBodyElement): NodeListOf<Element> {
                const nodeList = arrayFromCollection(
                    elementQuerySelectorAll.apply(this, ArraySlice.call(arguments) as [string])
                );
    
                if (!featureFlags.ENABLE_NODE_LIST_PATCH) {
                    const filteredResults = getFilteredArrayOfNodes(
                        this,
                        nodeList,
                        ShadowDomSemantic.Disabled
                    );
                    return createStaticNodeList(filteredResults);
                }
    
                return createStaticNodeList(
                    getFilteredArrayOfNodes(this, nodeList, ShadowDomSemantic.Enabled)
                );
            },
            writable: true,
            enumerable: true,
            configurable: true,
        },

});

Specifically this part:

    value(this: HTMLBodyElement): NodeListOf<Element>

What kind of syntax is this and what is it used for?

Dai
  • 141,631
  • 28
  • 261
  • 374
Michael Munta
  • 207
  • 2
  • 16

1 Answers1

0

That is the syntax of Typescript, a superset of JavaScript.

this: HTMLBodyElement indicates that the key word this will be used in the funciton to handle the current object on which we are working, and this object will be an HTMLBodyElement meaning the body tag of an html document.

:NodeListOf is to specify that the function will return a NodeList of DOM elements.

1001pepi
  • 114
  • 4
  • So, how would this function be called in JS? And how does it differ to the value from the previous object querySelector? – Michael Munta Nov 27 '21 at 15:56