Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jquery库中重新定义方法的最佳方法_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 在jquery库中重新定义方法的最佳方法

Javascript 在jquery库中重新定义方法的最佳方法,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,假设我想更改引导typeahead库中的render函数。 激活此目标的一种方法是: _.extend($.fn.typeahead.Constructor.prototype, { render: function (items) { // some code } }); 假设我只想为一个特定的元素重新定义渲染函数,比如 element.typeahead({ minLength: 3, source: getSource }); 我应该如何重

假设我想更改引导typeahead库中的render函数。

激活此目标的一种方法是:

_.extend($.fn.typeahead.Constructor.prototype, {
    render: function (items) {
      // some code
    }
});
假设我只想为一个特定的元素重新定义渲染函数,比如

element.typeahead({
    minLength: 3,
    source: getSource
});
我应该如何重新定义渲染函数,以便仅对该元素有效

附言:

我正在使用jquery和下划线

也许您可以先克隆它

$.fn.typeaheadCustom = $.fn.typeahead;

_.extend($.fn.typeaheadCustom.Constructor.prototype, {
    render: function (items) {
      // some code
    }
 });

element.typeaheadCustom({
    minLength: 3,
    source: getSource
});
更新

您可能需要对其进行深度克隆:

$.fn.typeaheadCustom = $.extend(true, $.fn.typeahead, {});

我尝试过,但它不起作用,因为
$.fn.typeahead.Constructor.prototype.render
已更改,您可能需要深度克隆它
$.fn.typeaheadCustom=$.extend(true,$.fn.typeahead,{})
也使
$.extend(true,$.fn.typeahead,{})它不工作。