1

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?

dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Apr 26, 2013 at 9:10

1 Answer 1

3

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/.

answered Apr 26, 2013 at 9:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.