在插件js中添加函数,而无需实际修改js文件

Add function to the plugin js without actually modifying js file

本文关键字:js 修改 文件 插件 添加 函数      更新时间:2023-09-26

我想在插件js中添加与现有函数"_select"相同级别的"setInputDataId"函数http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js文件,而不实际修改文件本身。所以我想知道如何添加新功能,我将感谢您的帮助。

插件中的现有代码:

    _select: function select(datum) {
            this.input.setQuery(datum.value);
            this.input.setInputValue(datum.value, true);
            this._setLanguageDirection();
            this.eventBus.trigger("selected", datum.raw, datum.datasetName);
            this.dropdown.close();
            _.defer(_.bind(this.dropdown.empty, this.dropdown));
        },

我想要的新代码:

    _select: function select(datum) {
            this.input.setQuery(datum.value);
            this.input.setInputValue(datum.value, true);
            this.input.setInputDataId(datum.raw);
            this._setLanguageDirection();
            this.eventBus.trigger("selected", datum.raw, datum.datasetName);
            this.dropdown.close();
            _.defer(_.bind(this.dropdown.empty, this.dropdown));
        },

此外,由于"setInputDataId"函数最初不在插件中,我也想添加这个函数。

功能体为:

      setInputDataId: function setInputDataId(raw) {
            this.$input.attr('data-id',raw.id);
        },

请转到http://twitter.github.io/typeahead.js/examples/在chrome的控制台选项卡中执行以下操作:

console.dir($.fn.typeahead)

现在展开节点函数,然后是",然后是第二个闭包,在那里你可以看到Typeahead。现在点击Typeahead原型,你可以看到select方法。如何修改此函数。

层次结构:

function
  <function scope>
    Closure (second)
      Typeahead
        prototype
          _select

As_只是一个对象,只需在添加上述文件后将其添加到脚本文件中即可。

例如:

_.functionName = function() { //implementation };

首先打开控制台,在那里试试。

输入_并点击回车键,您应该得到对象的内容。然后尝试上面的伪代码,然后出发!但很明显,除非你把它添加到某个文件中,否则它只会一直存在,直到你关闭浏览器。