If I return an object from an anonymous immediately invoked function expression where does it go? For example where does foo go in this code….
(function(){
var foo;
return foo;
})();
Does this have any useful applications?
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.
Returning a value from your self-executing function without assigning that return value to a result variable is just a return value that goes nowhere and thus creates no new references to the data in your function. So as soon as the function returns, the data will be garbage collected, the same as if you didn’t have the return statement. So, this:
is no different than returning a value from a function and not assigning the returned value to anything.
You would do this to capture the return value:
Working demo: http://jsfiddle.net/jfriend00/t9wJL/
As for the usefulness of returning a value. It’s useful if you assign the return value to a variable or use it as input into some other function, but accomplishes nothing if you don’t.