eventHandler末尾的Javascript调用函数
我有一个全局函数,它接受一个配置对象来显示一个参数化的对话框 这就是来源 我想这样称呼它eventHandler末尾的Javascript调用函数,javascript,jquery,Javascript,Jquery,我有一个全局函数,它接受一个配置对象来显示一个参数化的对话框 这就是来源 我想这样称呼它 var alert = { //the name is work in progress, don't worry info: function(text, okHandler){ modal({ content: text, buttons: { Ok: okHandler //here I want
var alert = { //the name is work in progress, don't worry
info: function(text, okHandler){
modal({
content: text,
buttons: {
Ok: okHandler //here I want to append automatically the close() function
}
});
},
error: ...
};
alert.info('Success!', function(){
doSomething();
//then close without me having to remember to do so
});
我想达到的目标可能吗?我正在研究
函数.prototype.call()
方法来实现这一点,但它是用于其他用途的这个问题困扰着我,因为我似乎仍然在编写不雅的代码。“我想现在我得到了正确的答案,”伊戈尔把我送到了正确的方向。
var alert = { //the name is work in progress, don't worry
info: function(text, okHandler){
modal({
content: text,
buttons: {
Ok: function() {
okHandler.apply(this);
//here I want to append automatically the close() function
close.apply(this);
}
}
});
},
error: ...
};
从handler()
返回的函数的作用域将是jQuery
eventHandler中的DOM元素,默认情况下将调用close()
,但不会多次写入
此时,您甚至可以调用handler()
而不是传递close
(注意括号),并获得相同的结果。很难理解基本问题,您能否在没有明确项目上下文的情况下从一个通用示例中解释您的问题
是否相同?请参见我对关闭行的更改
var alert = { //the name is work in progress, don't worry
info: function(text, okHandler){
modal({
content: text,
buttons: {
Ok: function() {
okHandler.apply(this);
//here I want to append automatically the close() function
close.apply(this);
}
}
});
},
error: ...
};
function handler(/*callback*/) {
var callback = arguments[0];
return function() {
if (callback != null) callback.apply(this);
close.apply(this, arguments);
};
}
function info(text, okHandler){
modal({
title: 'Info',
content: text,
buttons: {
Ok: handler(okHandler),
Close: close
}
});
}