在不传递参数的情况下调用函数meteor app [英] Calling a function without passing arguments meteor app
问题描述
通过发现流星(第 92 页),一节介绍了此代码:
Working through discover meteor (page 92), one section covers this code:
Posts.allow({
update: ownsDocument,
remove: ownsDocument
});
ownsDocument = function(userId, doc) {
return doc && doc.userId === userId;
}
您可能已经猜到的代码本身只允许用户更新/删除他们自己的帖子,具体取决于ownsDocument"是返回真还是假.当它似乎没有将参数传递给函数时,我不明白简单地调用ownsDocument"是如何成功完成任何事情的.当调用 Posts.allow 时传递 userId/doc 时,ownsDocument 如何返回 true 或 false?
The code itself as you may have guessed only allows a user to update/remove their own posts depending on if "ownsDocument" returns true or false. What I don't get how simply calling "ownsDocument" successfully accomplishes anything when it doesn't appear to be passing arguments to the function. How can ownsDocument return true or false when it doesn't appear Posts.allow is passing the userId/doc when it's being called?
删除示例代码中的杂散分号
removed stray semicolon in example code
推荐答案
[...] 当它没有出现时 Posts.allow 在被调用时正在传递 userId/doc?
[...] when it doesn't appear Posts.allow is passing the userId/doc when it's being called?
它在哪里被调用?
在您的示例中,ownsDocument
根本没有被称为.您所做的只是配置在 update
和 remove
情况下应该调用哪些函数.
In your example, ownsDocument
is not called at all. All you are doing is configuring which functions should be called in the update
and remove
case.
这些函数在别处和在不同的时间被调用,在那里它们将被传递正确的参数.
The functions are called somewhere else and at a different time, and there they will be passed the right arguments.
就像事件处理程序一样.foo.onclick = bar;
不会调用 bar
.它将函数的引用分配给 foo.onclick
,以便该函数可以在一段时间后被调用.并且在那个时刻,该函数将被传递一个事件对象.
It's just like with event handlers. foo.onclick = bar;
doesn't call bar
. It assigns a reference to function to foo.onclick
, so that the function can be called some time later. And at that moment, the function will be passed an event object.
这篇关于在不传递参数的情况下调用函数meteor app的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!