Name of the named function expression and the name of the function declaration

Source: Internet
Author: User

1 vars = 0,2b = 0;3 functionA (s) {4A =function () {5Console.log (S + b++);6     };7 Console.log (A);8Console.log (s++);9 }TenA (1); OneA (2); AConsole.log (DeleteA); -A (2);

A as a property of a global object, the value is a reference to a function, when a function is executed, an anonymous function is passed to a, then a scope chain lookup is made, a is found in the global object, and then assigned to it. Because a is a function declaration, it cannot be deleted. If you change it to the following:

1 vars = 0,2b = 0;3 functionA (s) {4D =function () {5Console.log (S + b++);6     };7 Console.log (d);8Console.log (s++);9 }TenA (1); OneD (2); AConsole.log (DeleteD);//true -D (2);

Note that D is not a variable, but is added to the global object as a property, the variable cannot be deleted, and the property can be deleted.

1 functionA () {2     functionB () {3         functionC () {4A =function () {5Console.log (1);6             };7         }8 C ();9     }Ten B (); One } A A (); -A ();//1

function declarations and function "true" names of named function expressions are different. In the above example, a is a function declaration that exists as a property of a global object and executes an assignment statement that is a in the global object that is found through the scope chain.

1(functionA () {2     functionB () {3         functionC () {4A =function () {5Console.log (1);6             };7         }8 C ();9     }Ten B (); One })(); AA ();//referenceerror:a is not defined

The name of the named function expression exists in a special object, which is between the parent scope and the A function scope, in the above example, the equivalent of adding a special object between the global object and the active object of a function, the special object has only one property that is a, the value is a function. This is why accessing the A property in a global object does not exist, only within the scope of the a function to access the A property. Other words:

1 function A () {}
1 (function A () {}) ();

The first of the above is a property that exists in the global object, and the second above is a special object that exists between a global object and the active object of a.

1 function A () {2     var A = 2; 3     Console.log (A); 4 }5 A (); // 2 6 A (); // 2

So, a variable or function with the same name as the function can be declared in the body of the function . It's just not recommended, although the syntax is correct.

1 function A () {2     function A () {3         console.log (2); 4     }5    A (); 6 }7 A (); // 2 8 A (); // 2

Name of the named function expression and the name of the function declaration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.