# scope and scope chain #
# 1. Scope # #
Definition: When a function is declared, the scope of the current function is determined, so-called scopes are the scope of the action (site)
Global scope:
A. window is the top-level object, which is the end point of all functions, variables, objects,
B. So a function or variable that is defined at the global scope is a property or method that belongs to the Window object.
C. Its scope is global and can be invoked anywhere.
Local scope:
A. When a function is declared, the scope inside the function is the local scope,
B. Variables and declarative functions declared through Var inside a function are private variables or functions of the function.
C. In general, a private variable or function inside a function cannot be accessed outside the function
Scope Chain:
A. When a variable is called inside a function, it is found in the current scope whether the variable is declared
B. If it is not declared, it will go to its previous scope to find
C. Until the global scope is found, or not if it is not define
The relationship between scope and execution context:
var a = ten, B = 10;
function fn (x) {
var a = 100,c = 200;
function bar (x) {
var a = 1000,d = 4000;
}
Bar (100)
Bar (200)
}
FN (10)
Global scope: Save a = 10,b = 10 in the context ring
fn Scope: Within the context ring save a = 100,x = ten, c = 200
Bar Scope: Inside the context ring save a = 1000,d =4000,x = 100 (First call) 200 (second call)
Scope and scope chain-javascript object-oriented advanced