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

时间:2014-03-04 06:22:30

标签: javascript

我认为这是一个初学JavaScript问题。这是一段代码(取自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;
    }
});

我相信对此有一个简单的解释。我该如何解决这种混乱?

2 个答案:

答案 0 :(得分:1)

实际上,这不是一个功能(据我所知)。在JS中,如果要将事件与函数绑定,则必须指定对这些函数的引用。尽管引用了一个函数,您可以创建一个self executing function本身! 所以,你也可以这样做:

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

然而,在

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

您正在将函数的结果赋给变量,并且您没有将函数绑定到事件。但是,您也可以使用自执行功能,如下所示:

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

答案 1 :(得分:1)

我不理解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;
    }
});