JavaScript variable Masking

Source: Internet
Author: User

Variables with the same name in different scopes trigger a variable mask:

{Let x= {color: "Blue"}; Let y=x; Let Z= 3; {        //x is defined repeatedly, so a variable mask is made to the global variable xLet x = 5;        Console.log (x); //y is not redefined, so the variable y.color unchangedConsole.log (Y.color); //x has been redefined, so x.color does not exist, undefinedConsole.log (X.color); //is directly the modification of the object Y,        //Let y = {color: ' red '} before the reference to Y will error undefinedY.color = "Red";    Console.log (z); } console.log (X.color);//RedConsole.log (Y.color);//RedConsole.log (z)//3}

The scope chain. If the definition of a variable is not found in the scope, it is up to the previous level, and if a variable with the same name is already defined in that work, the previous level is masked. and must follow the principle of first declaration and reuse.

JavaScript variable Masking

Related Article

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.