Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can’t I assign values to a variable inside a named function expression with the same name?

This is a named function expression with the name test. Inside, I assign 123 to a variable, also named test. Then test is logged. The function prints its body in the console, but not 123. What is the reason for such behavior?

(function test() {
  test = 123;
  console.log( test );
}());

Where does my explanation of function execution fail?

  1. Start of function execution: test is a local variable that references the function itself
  2. Local variable test is reassigned to number 123
  3. console.log(test) shows the number 123.
like image 875
Roman Belyaev Avatar asked Jun 03 '14 17:06

Roman Belyaev


People also ask

Can a function and a variable have the same name JavaScript?

A function declaration also creates a variable with the same name as the function name. Thus, unlike those defined by function expressions, functions defined by function declarations can be accessed by their name in the scope they were defined in, as well as in their own body.

Can you assign values to a function?

No, you can't assign values to functions. You can assign values to function pointers, but not to functions in C++.

What happens when you assign a value to a variable that has not been declared it will automatically become a global variable?

Automatically Global If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. This code example will declare a global variable carName , even if the value is assigned inside a function.

What happens when you assign a value to a variable that has not been declared?

If you assign a value to a variable that you have not declared with var , JavaScript implicitly declares that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function.


1 Answers

I believe this piece of the ecma spec explains this behavior. This relates specifically to named Function Expressions

The production

FunctionExpression : function Identifier ( FormalParameterListopt ) { FunctionBody }

is evaluated as follows:

  1. Let funcEnv be the result of calling NewDeclarativeEnvironment passing the running execution context’s Lexical Environment as the argument
  2. Let envRec be funcEnv’s environment record.
  3. Call the CreateImmutableBinding concrete method of envRec passing the String value of Identifier as the argument.
  4. Let closure be the result of creating a new Function object as specified in 13.2 with parameters specified by FormalParameterListopt and body specified by FunctionBody. Pass in funcEnv as the Scope. Pass in true as the Strict flag if the FunctionExpression is contained in strict code or if its FunctionBody is strict code.
  5. Call the InitializeImmutableBinding concrete method of envRec passing the String value of Identifier and closure as the arguments.
  6. Return closure.

The use of CreateImmutableBinding when creating the scope of a named Function Expression, creates the identifier (in this case test) as an immutable variable. That is why assignment to it does not change its value.

like image 52
James Montagne Avatar answered Sep 20 '22 21:09

James Montagne