jquery如何重写类方法?
看起来这个问题很流行,但是没有jQuery版本 我有两门课,第二门是从第一门扩展而来的 扩展方法 据 第二个对象的方法应该使用jquery如何重写类方法?,jquery,overriding,extend,Jquery,Overriding,Extend,看起来这个问题很流行,但是没有jQuery版本 我有两门课,第二门是从第一门扩展而来的 扩展方法 据 第二个对象的方法应该使用$.extend()覆盖第一个对象的方法(使用相同的名称) 然而,我在html中检查它,发现第一个对象的函数仍然可以正常工作。(警报“first”…)这使得其子类无法重写该函数 所以我想问,这是怎么发生的,怎么解决的。我想提醒“第二个”…首先扩展,然后设置新的setOutput(因此覆盖扩展方法) 如果你改变顺序,它就会工作 function UIButton(o){
$.extend()
覆盖第一个对象的方法(使用相同的名称)
然而,我在html中检查它,发现第一个对象的函数仍然可以正常工作。(警报“first”…)这使得其子类无法重写该函数
所以我想问,这是怎么发生的,怎么解决的。我想提醒“第二个”…首先扩展,然后设置新的
setOutput
(因此覆盖扩展方法)
如果你改变顺序,它就会工作
function UIButton(o){
this.setOutput=function(divname){
alert("first");
}
if (typeof(o) != 'undefined') $.extend(true, this, o);
}
function UIButton2(o) {
this.setOutput=function(divname) {
alert("second");
}
if (typeof(o) != 'undefined') $.extend(true, this, o);
return new UIButton(this);
}
(对于本例,只需要在ui按钮中使用,但我将其添加到这两个按钮中以备将来使用)
在的演示中,您可以使用jQuery代理方法覆盖类
参考:
比如说
(function() {
// log all calls to setArray
var proxied = jQuery.fn.setArray;
jQuery.fn.setArray = function() {
console.log( this, arguments );
return proxied.apply( this, arguments );
};
})();
(function() {
// log all calls to setArray
var proxied = jQuery.fn.setArray;
jQuery.fn.setArray = function() {
console.log( this, arguments );
return proxied.apply( this, arguments );
};
})();