如何在JavaScript/jQuery中合并两个函数?
我有以下资料:如何在JavaScript/jQuery中合并两个函数?,javascript,jquery,Javascript,Jquery,我有以下资料: var firstFunc = function (settings){ alert('sup'); }; var secondFunc = function (settings){ alert('dude'); }; $.extend(firstFunc, secondFunc); firstFunc(); 我想让最后一个firstFunc()返回sup,然后是dude 上下文 原因是我实现了一个继承模式,其中,如果由派生对象定义,则基本对象将覆盖某些功
var firstFunc = function (settings){
alert('sup');
};
var secondFunc = function (settings){
alert('dude');
};
$.extend(firstFunc, secondFunc);
firstFunc();
我想让最后一个firstFunc()返回sup,然后是dude
上下文
原因是我实现了一个继承模式,其中,如果由派生对象定义,则基本对象将覆盖某些功能:
if (child.close){
base.close = child.close;
}
base.close = function() {
// do stuff
}
问题是,我总是需要基本对象来“做事情”,我想将child.close的功能附加到base.close。我怎样才能做到这一点
Fiddle:
$。extend
用于对象,而不是函数。为什么不做一个调用前两个函数的第三个函数呢
function bothFuncs(){
firstFunc();
secondFunc();
}
如果不知道确切的继承实现,很难回答,但类似的内容可能会帮助您:
// reference to child instance
var self = this;
// overwrite close method
this.close = (function() {
var baseClose = self.close;
return function() {
baseClose.call(self);
alert('child close');
};
})();
后来:
base.close(); // alert('base close');
child.close(); // alert('base close'); -> alert('child close');
创建包装器方法的想法
演示:此函数只连接对象,不连接函数。