简体   繁体   English

什么时候JavaScript中需要function()块?

[英]When is the function() block necessary in JavaScript?

I think this is a beginner JavaScript question. 我认为这是一个初学JavaScript问题。 Here's a piece of code (taken from Discover Meteor ) to illustrate: 这是一段代码(取自Discover Meteor )来说明:

Meteor.methods({
    // why function is necessary here?
    post: function(postAttributes) {
        var user = Meteor.user(),
            // why is not necessary here?
            postWithSameLink = Posts.findOne({
                url: postAttributes.url
            });
        // ensure the user is logged in
        if (!user)
            throw new Meteor.Error(401, "You need to login to post new stories");
        // ensure the post has a title
        if (!postAttributes.title)
            throw new Meteor.Error(422, 'Please fill in a headline');
        // check that there are no previous posts with the same link
        if (postAttributes.url && postWithSameLink) {
            throw new Meteor.Error(302,
                'This link has already been posted', postWithSameLink._id);
        }
        // pick out the whitelisted keys
        // and why not below here?
        var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), {
            userId: user._id,
            author: user.username,
            submitted: new Date().getTime()
        });
        var postId = Posts.insert(post);
        return postId;
    }
});

I believe there is a simple explanation for this. 我相信对此有一个简单的解释。 How do I solve this confusion? 我该如何解决这种混乱?

Actually, that's not a function (as much as I know). 实际上,这不是一个功能(据我所知)。 In JS, if you want to bind events with functions, you have to specify references to those functions. 在JS中,如果要将事件与函数绑定,则必须指定对这些函数的引用。 In spite of referring a function, you can create a self executing function itself! 尽管引用了一个函数,您可以自己创建一个self executing function So, you could have done this too: 所以,你也可以这样做:

Meteor.methods({
    // why function is necessary here?
    post: myFunction
});

function myFunction(postAttributes) {
            var user = Meteor.user(),
                // why is not necessary here?
                postWithSameLink = Posts.findOne({
                    url: postAttributes.url
                });
            // ensure the user is logged in
            if (!user)
                throw new Meteor.Error(401, "You need to login to post new stories");
            // ensure the post has a title
            if (!postAttributes.title)
                throw new Meteor.Error(422, 'Please fill in a headline');
            // check that there are no previous posts with the same link
            if (postAttributes.url && postWithSameLink) {
                throw new Meteor.Error(302,
                    'This link has already been posted', postWithSameLink._id);
            }
            // pick out the whitelisted keys
            // and why not below here?
            var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), {
                userId: user._id,
                author: user.username,
                submitted: new Date().getTime()
            });
            var postId = Posts.insert(post);
            return postId;
        }

However, in 但是,在

postWithSameLink = Posts.findOne({
                        url: postAttributes.url
                    });

you are assigning the result of a function to a variable, and you're NOT binding a function to an event. 您正在将函数的结果赋给变量,并且您没有将函数绑定到事件。 However, you can use a self executing function there too, like this: 但是,您也可以使用自执行功能,如下所示:

postWithSameLink = function()
                   {
                    return Posts.findOne({ url: postAttributes.url});
                   }

I dont understand Meteor but will try to answer you, the answer is in comments 我不明白Meteor但会试着回答你,答案就在评论中

Meteor.methods({
    // why function is necessary here? Because this is a function definition
    // we are defining a function called post in the object Meteor.methods
    // post in Meteor.methods is a function so we are specifying that.
    post: function(postAttributes) {
        var user = Meteor.user(),
            // why is not necessary here? Because this is a function call
            // you are probably looking for posts with same link using an 
            // existing function "Posts.findOne" you dont need to specify 
            // its a function, its already done somewhere else when 
            // defining Posts.findOne
            postWithSameLink = Posts.findOne({
                url: postAttributes.url
            });
        // ensure the user is logged in
        if (!user)
            throw new Meteor.Error(401, "You need to login to post new stories");
        // ensure the post has a title
        if (!postAttributes.title)
            throw new Meteor.Error(422, 'Please fill in a headline');
        // check that there are no previous posts with the same link
        if (postAttributes.url && postWithSameLink) {
            throw new Meteor.Error(302,
                'This link has already been posted', postWithSameLink._id);
        }
        // pick out the whitelisted keys
        // and why not below here? beacuse this is a case of recursive function calls
        // Here you are calling a function called "pick" which is a part of 
        // the object "_" it probably picks the "Post" with such and such credentials
        // and returns the result to the function "extend" which is also part of 
        // object "_". extend will then probably do some transformation to the 
        // result and assign the returned result to the variable "post"
        // note that var post is different from the function post above, dont confuse em
        var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), {
            userId: user._id,
            author: user.username,
            submitted: new Date().getTime()
        });
        var postId = Posts.insert(post);
        return postId;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM