【问题标题】:Function as an object property [duplicate]作为对象属性的功能[重复]
【发布时间】:2022-01-05 04:37:17
【问题描述】:

有人可以从这个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,
        },

});

具体这部分:

    value(this: HTMLBodyElement): NodeListOf<Element>

这是什么语法,有什么用途?

【问题讨论】:

标签: javascript


【解决方案1】:

这是 JavaScript 的超集 Typescript 的语法。

this: HTMLBodyElement 表示关键字 this 将在函数中用于处理我们正在处理的当前对象,该对象将是 HTMLBodyElement,表示 html 文档的正文标记。

:NodeListOf 是指定函数将返回一个 DOM 元素的 NodeList。

【讨论】:

  • 那么,这个函数在JS中怎么调用呢?以及它与之前的对象 querySelector 的值有何不同?
猜你喜欢
  • 1970-01-01
  • 2019-06-28
  • 2021-06-29
  • 2018-04-21
  • 2020-12-18
  • 2011-09-29
  • 2022-01-14
  • 2016-09-09
  • 2021-09-10
相关资源
最近更新 更多