Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
eventHandler末尾的Javascript调用函数_Javascript_Jquery - Fatal编程技术网

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
        }
    });
}