Javascript 在jquery库中重新定义方法的最佳方法
假设我想更改引导typeahead库中的render函数。 激活此目标的一种方法是: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 }); 我应该如何重
_.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,{})代码>它不工作。