Skip to content Skip to sidebar Skip to footer

Confusion About Function Declaration

If I assign a named function to a variable, why I can't access to the named function: var a = function b() { console.log('Hello World'); } a() // hello world; b() // b is not d

Solution 1:

If you use a named function expression (not a function declaration!), the name of the function is only accessible form inside the function.

From the specification ("Identifier" refers to the name of function, i.e. function Identifier() {}):

The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.


but I can delete b

No, you can't. When you pass anything that is not a reference to delete or the reference cannot be resolved, it will return true.


This is a great article about all the function definition stuff: http://kangax.github.io/nfe/.


Post a Comment for "Confusion About Function Declaration"