Talking about the variable name and function name in js, talking about the js function name duplication

Source: Internet
Author: User

Talking about the variable name and function name in js, talking about the js function name duplication

Today, skokai asked a question about variable name conflicts. I felt very interesting. By the way, I also reviewed some of the pre-resolution knowledge. If there is something wrong, I forgot my predecessor's guidance. The question is as follows:

Var a = 100; function a () {console. log (a) ;}a ();

After the code is executed, an error is returned: a is not a function.

Why is this error reported? This involves pre-parsing functions and variables:

1) function declaration will be set to the top

2) The variable Declaration will also be set to the top

3) function declaration is more advanced than variable Declaration: (the function is above the variable)

4) variables and value assignment statements are written together. During parsing by the js engine, the variables are split into two parts: Declaration and value assignment. The declaration is set to the top, and the value assignment is kept in the original position.

5) declared variables are not repeated.

The above code is equivalent:

Var a = function () {console. log (a);} var a = 100; ();

It is equivalent to re-assigning a value to a, so an error is reported.

The above discussion about variable names and function names in js is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.