Things that are easy to ignore (variable elevation and function elevation)

Source: Internet
Author: User

1, variable promotion: As long as the variable is declared in the code, no matter where it is declared, the JS engine will put its declaration at the top of the scope;

// undefined var A;

2, function Promotion: As long as the function is declared in the code, no matter where it is declared, the JS engine will put its declaration at the top of scope scopes;

// function B () {} function B () {};

As you can see, a variable or function declaration is not placed at the top of the current scope, so the question is: if the variable and function have the same name, is it ripe and heavy? Who has a higher priority level?

varA;functionA () {};console.log (a);//function A () {}functionB () {};varB;console.log (b);//function B () {}varC=1;functionC () {}console.log (c);//1functiond () {};varD=1; Console.log (d);//1

Summary: When variable declarations and function declarations have duplicate names,

Regardless of the order in which they are declared, the function declaration overrides the variable declaration, that is, when the value of a is declared function A () {}.

Note: If a is initialized at the same time as a variable declaration, or a is subsequently assigned, the value of a is the value of the variable.

In other words: a===undefined? a=== function: a=== variable

Things that are easy to ignore (variable elevation and function elevation)

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.