wyc
wyc

Reputation: 55273

When is the function() block necessary in JavaScript?

I think this is a beginner JavaScript question. Here's a piece of code (taken from Discover Meteor) to illustrate:

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?

Upvotes: 0

Views: 69

Answers (2)

sabithpocker
sabithpocker

Reputation: 15566

I dont understand Meteor but will try to answer you, the answer is in comments

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

Upvotes: 1

Sayed
Sayed

Reputation: 631

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. In spite of referring a function, you can create a self executing function itself! 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});
                   }

Upvotes: 1

Related Questions