【问题标题】:how to add additional parameters to a callback function in javascript如何在javascript中向回调函数添加附加参数
【发布时间】:2013-12-18 13:07:06
【问题描述】:

在我的 javascript XMPP 项目中,有一个核心部分和一个插件部分。核心部分必须独立于插件部分。我试图从插件部分调用一个核心函数,它本身需要调用一个插件函数。由于概念的核心部分不知道任何插件函数,我试图将插件函数作为回调函数移交。就是这样:

strophe-librarymuc-plugin 包含join-function

我自己在核心中添加了一个函数joinMuc,这样就可以有回调了,这是原函数没有提供的:

core = {
    joinMuc: function(room, onMessage, onPresence, onRoster, result) {
        MX.connection.muc.join(
            room,
            jQuery.jStorage.get('settings').username,
            onMessage,
            onPresence,
            onRoster
        );
        if (result) result(room);
    },
}

我调用core.joinMuc并传递参数和回调函数:

plugin = {
    doHtml: function(result) {
        //…
    },
    doPlain: function(result) {
        //…
    },
    joinGame: function(chatroom){
        core.joinMuc(
            chatroom,
            core.onMessage,
            core.onPresence,
            core.onRoster,
            function(result){
                //…
            }
        );
    }
}

core.onMessage 将从join-function 获取实际的 xmpp-message 作为参数,但现在这里是棘手的部分:我实际上需要 core.onMessage 的三个参数来交出两个插件功能其中一个应该在core.onMessage 决定它是什么类型的消息之后运行:

core = {
    onMessage: function (message, handleHTMLMessage, handlePlainMessage){
        var rawMessageBody = $(message).find('body').text();
        var parsedMessageHtml = $.parseHTML(rawMessageBody)[0];
        if ( parsedMessageHtml.nodeName.toLowerCase() == 'foo' ) {
            handleHTMLMessage(parsedMessageHtml);
        } else {
            handlePlainMessage(rawMessageBody);
        }
        return true;
    },
}

所以现在我的问题是:如何使用两个额外的参数调用core.onMessage?我试图调用core.onMessage(plugin.doHtml,plugin.doPlain) 作为core.joinMuc 的参数,但最终导致浏览器崩溃的无限循环。

【问题讨论】:

    标签: javascript parameters callback parameter-passing strophe


    【解决方案1】:

    您应该能够使用匿名函数,该函数最终使用所需参数调用 core.onMessage

    core.joinMuc(
        chatroom,
        function(message) {            
            core.onMessage(message, plugin.doHtml, plugin.doPlain);
        },
        core.onPresence,
        core.onRoster,
        function(result){
            //...
        }
    );
    

    这是你想要的吗?

    【讨论】:

    • 好的,谢谢。见my other comment
    • 第一次发布时我没有发表评论,因为我不确定我是否理解这个问题。但是假设我理解正确,并且问题是第三方工具正在调用作为第三个参数提供给MX.connection.muc.join的函数,但只用一个参数调用它,那么我不认为这种技术帮助任何人。此函数仍将使用相同的单个参数调用。还是我错过了什么?
    • 不,它会被传入的任何东西调用。这本质上与你所拥有的类似,除了我已经定义了函数内联。在您的示例中,它调用一个返回新函数的函数。在您的情况下,createMessageHandler 会立即被调用,因此它的行为本质上是相同的。不过我确实对参数进行了更改。
    【解决方案2】:

    一种可能性:

    // probably namespaced somewhere, but this is the idea
    var createMessageHandler = function(handleHTMLMessage, handlePlainMessage) {
        return function(message) {
            core.onMessage(message, handleHTMLMessage, handlePlainMessage);
        };
    };
    

    然后:

    plugin = {
        doHtml: function(result) { /* ... */},
        doPlain: function(result) { /* ... */},
        joinGame: function(chatroom){
            core.joinMuc(
                chatroom,
                createMessageHandler(plugin.doHtml, plugin.doPlain),
                //…
            );
        }
    }
    

    我不是特别喜欢 plugin 内部对 plugin 的内部引用,并且可能会进行重构以避免它们,但这可能是一种简单的方法。

    【讨论】:

    • 感谢您的建议,这很有帮助。我接受了这个作为答案,尽管 Vivins 解决方案也可以,但由于该项目相当科学或具有教育意义,所以这个更“冗长”,因此更合适。
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 2014-12-29
    • 1970-01-01
    • 2023-03-07
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    相关资源
    最近更新 更多