Use of this:
1 var oul = document.getElementById ("UL1"); 2 var aLi = oul.getelementbytagname ("li"); 3 for (var i=0;i<10;i++) 4 {5 function () {6 ali[i].style.width = "200px;" 7 }; 8 }
This piece of code is wrong
First of all, the scope chain in JavaScript and the prototype chain scope are for variables, suppose we create a function, the function contains a function, then there are now three scopes namely: global scope, function 1 scope, function 2 scope
When the code executes, if the relevant variable is not found in the scope of the current function, go up one level until it is found. Note that functions and functions are nested relationships. As for the prototype chain, it is related to the constructor function, and later.
To perform the context:
When the JavaScript code file is loaded by the browser, the default first entry is a global execution context. When a function is called in the global context, the program flow enters the called function, and the engine creates a new execution context for the function, and presses it into the top of the execution context stack. The browser always executes the current context at the top of the stack, and once executed, the context is ejected from the top of the stack and then into the context under which the code executes. The context in the stack is then executed sequentially and the stack pops up until it returns to the global context.
Execution context Creation Process: This process is divided into two stages:
1. Build phase (occurs when a function is called, but before executing the specific code in the body of the function)
Handle arguments, arguments, and then declarations of the function, and finally the declaration of the variable (the variable is simply declared, the assignment operation executes, but the function declaration performs an assignment, and the function expression is declared without assigning a value)
Creating a Scope chain
Determine the value of this
2. Code Execution phase:
Assigning values to variables
Function reference
Execute other code
The cause of the code error at the beginning is because the for loop has already ended when the event occurred, and I is the I for the last loop of the For loop, not the events in the I,for loop that occurred when the event was just loaded, which tells the browser to do something in the back function when this event occurs. Use this pointer to perform the current operation when an event occurs
1 var oul = document.getElementById ("UL1"); 2 var aLi = oul.getelementbytagname ("li"); 3 for (var i=0;i<10;i++) 4 {5 function () {6 this. Style.width = "200px;" 7 }; 8 }
JavaScript execution context