Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What is the difference between a function expression vs declaration in Javascript?
I am aware of the differences between Function Declarations and Expressions, but have come across this code involving function name and want to understand what happens when we run it:
var abc = function def() {
console.log("Wait! What??");
}
I know that this is not a way to JavaScript, but just want to know few things:
- What happens to
abc? Why it works?abccan be called but notdef, why? - Is it a function declaration or an expression?
defisundefined– why? If it is supposed to be, are there
memory leaks?- Why is
abc.prototypeis functiondef?
Thanks
It contains a function object. If you are doing nothing with it, it will be garbage-collected.
Why not? What “works”?
This is only true from outside, and not in IE. See below.
It is a function expression. You can easily see that as it is part of an assignment expression; declarations always need to be on top level (of functions or global code)
Only from outside. A function expression does not create variables. “def” is the name of the function, and inside the function it is a reference to the function as well. This allows recursion for example without using any outer variables.
Yes, in Internet Explorer. It creates two distinct functions from that code. For the details, see http://kangax.github.com/nfe/#jscript-bugs
It is not. It is just an object. Maybe it is shown with that name in your console, as belongs to a function named “def”.