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 defined
b // b is not defined
and at this time, I can not delete the a
variable, but I can delete b
, when I delete the b
, the a
's function can still there
delete a //false
delete b //true
a() //Hello World
the b
function isn't just referenced by the a
, not copy, so why the a
function is still there?
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/.