What is the difference between these two?
collapse: function(fold)
{
...
...
}
and
function collapse(fold)
{
...
...
}
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.
The first one outside of the context of an object literal is a syntax error.
However, I believe you are asking about the difference between a function expression and a function declaration.
Your first one is a function expression. You assign an anonymous function to a variable. Its variable definition is hoisted to the top of its scope, but not the assignment of the function.
The second is the function declaration. Its entire body is hoisted to the top of the scope.
In general, a function expression is often used as it is more expressive. You can give it a name if you need to call it recursively (or for better detailed stack traces), but remember IE leaks this name to the outer scope.
Further Reading.