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