JavaScript variable Promotion

Source: Internet
Author: User

This article starts in Segmentfault, if has the reprint quoted, must be sure the private messages informs, and indicates the source: https://segmentfault.com/q/1010000005921127

    1. The problem with variables is that there are two steps to declaring and assigning values, and the two steps are separate.

    2. When a function declaration is promoted, the Declaration and assignment of two steps are promoted, while the normal variable can only elevate the declaration step, not the assignment step.

    3. Once the variable has been promoted, the declaration step is executed uniformly once for all objects that are lifted, and then an assignment step is performed on the variable once. When performing the assignment step, the assignment step of the function variable is executed first, and then the assignment step of the normal variable is executed.
      When you understand these three points, everything will be enlightened.

First Look at a demo:

(function(){                function a(){}; var a; alert(typeof a); //function })();

Raise two a first, then perform the function assignment step, A is not assigned, so the result is function

Look at one more:

 (function(){        alert(typeof a);//function function a(){}; var a = 1; })();

First, raise two A, and then execute the function's assignment step, because before the alert statement execution, the assignment step of a = 1 is not executed, the function is not overwritten, therefore functions

One of the most convincing:

(function(){             var a = 1; function a(){}; alert(typeof a); //number })();

Before the alert statement executes, the A = 1 step and the function assignment step are executed, and the function is still output number after the A = 1 assignment statement, because the function's assignment step precedes the assignment step of a = 1, the function is overwritten, and the number is output.

All the code that is related to the variable promotion is not clearly explained with these three points.
Above.
Welcome to shoot Bricks ...

JavaScript variable Promotion

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.