调用一个不传递参数的函数

Calling a function without passing arguments meteor app

本文关键字:参数 函数 一个 调用      更新时间:2023-09-26

通过发现流星(第92页),有一节涵盖了以下代码:

Posts.allow({
    update: ownsDocument,
    remove: ownsDocument
});
ownsDocument = function(userId, doc) {
    return doc && doc.userId === userId;
}

代码本身,你可能已经猜到,只允许用户更新/删除自己的帖子取决于是否"ownsDocument"返回真或假。我不明白的是,简单地调用"ownsDocument"如何成功地完成任何事情,当它似乎没有传递参数给函数。ownsDocument如何返回true或false时,它不出现的帖子。当它被调用时,允许传递userId/doc ?

编辑:删除示例代码中的多余分号

Meteor自动将userIddoc参数传递给您分配给传递给allowupdateremove键的任何函数对象。您发布的代码的工作原理相同:

Posts.allow({
    update: function(userId, doc) {
        return doc && doc.userId === userId;
    },
    remove: function(userId, doc) {
        return doc && doc.userId === userId;
    }
});

ownsDocument函数在代码中没有被调用。allow函数通知Meteor在Post被更新或删除时调用该函数。

[…当它不显示帖子时。当它被调用时,允许传递userId/doc ?

它在哪里被调用?

在您的示例中,ownsDocument根本不被称为。您所要做的就是配置在updateremove情况下应该调用哪些函数。

函数在其他地方和在不同的时间调用,在那里它们将被传递正确的参数。


这就像事件处理程序一样。foo.onclick = bar;不调用bar。它给foo.onclick分配了一个对function的引用,以便稍后调用该函数。此时,函数将被传递一个事件对象