What’s the difference between these functions? Thanks for reply!
Function #1
var myQuery = (function() {
(...)
})();
Function #2
var myQuery = (function() {
(...)
});
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the first case, you’re self-invoking a function literal and assigning the value of the invocation to the variable
myQuery.In the second case, you’re assigning a reference to the anonymous function that you’ve defined. Here,
myQueryacts like a pointer or a reference to a function.To better illustrate this.
In this case,
myQuerycontains the valueHello. Now if you had:myQuerycontains a reference to the function. If you usedconsole.login Firebug to output this value, you would seefunction(). This reference is something you can pass around or even invoke. So:Now,
valuewill containHello. Hope this explains the difference.