2014-03-04 29 views
0

我认为这是一个初学JavaScript问题。下面是一段代码(从Discover Meteor拍摄)来说明:什么时候在JavaScript中需要function()块?

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

我相信这是一个简单的解释这一点。我如何解决这个困惑?

+0

检查[如何匿名函数JavaScript工作](http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work),[闭包](https://developer.mozilla.org/en -US/docs/Web/JavaScript/Guide/Closures)和[函数和函数范围](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope)。 –

回答

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

我不明白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; 
    } 
}); 
相关问题