【发布时间】:2014-07-21 03:22:54
【问题描述】:
这与另一个 SO Q&A Override jQuery functions 有关,但不是重复。
从上述问题的答案中可以清楚地看出,覆盖 jQuery 函数的模式是:
(function($){
// store original reference to the method
var _old = $.fn.method;
$.fn.method = function(arg1,arg2){
if ( ... condition ... ) {
return ....
} else { // do the default
return _old.apply(this,arguments);
}
};
})(jQuery);
但是为什么!?
我已经能够简单地通过在$.extend 或$.fn.extend 中定义一个与要被覆盖的函数同名的函数来覆盖一个jQuery 函数。
考虑一下:
// random example showing jquery function overriding
$.fn.extend({
hide: function() {
$(this).css({"color":"red"});
}
});
$("#test").hide(); // this will actually paint the #test element red!
我想了解为什么_old.apply(this,arguments) 是覆盖 jQuery 函数的首选方式,如 here 和 here 所列。
【问题讨论】:
-
那么你遇到了什么问题?
-
fn是 jQuery 对prototype的快捷方式,因此您只需执行$.fn.hide(或$.prototype.hide)即可创建一个名为hide的新原型属性,该属性将覆盖之前的hide,并且要了解您可能应该前往 MDN 并阅读有关对象和原型的内容,因为这与 jQuery 关系不大。 -
问题是我错过了证明
.apply将是覆盖 jQuery 函数的首选方式的原因。 -
显然 jQuery 参数是在数组中提供的。而不是覆盖我会使用中间人:tech.zumba.com/middleman.js
标签: javascript jquery