The steps performed by the function

  • Form a private execution context (AO) and then stack execution
  • Initialize the scope chain
  • Initialize THIS
  • To initialize the ARGUMENTS
  • Parameter value
  • Variable ascension
  • Code execution
  • Decide whether to unload the stack or not as the case may be

scope

When a function is created, its scope is declared, and the context in which it is created is the context in which it is scoped

The relationship between scope and context

  • It’s one thing, it’s all the space that the function is executing
  • Scope of FN [ECG] THE ECG is also a global context
  • It’s all stack memory at the core
  • The EC(FN) context formed by the function execution itself
  • A function is created with its scope declared

Function execution forms a “new” “private” execution context and is then pushed to execution

Normally, after the function is executed, the resulting private context is released from the stack to optimize memory space

EC(FN) private execution context

AO(FN) Private variable object [where variables declared by private context are stored => private variables]

  • AO[active Object] is a branch of VO, function private context [AO]
  • Private variable (AO)
    • At sign 1 parameter
    • The declared variable in the body of the @2 function

The steps before code execution

  • Scope chain
    (fn),ec(g)>
  • Initialize THIS
  • Initial ARGUMENTS- Collection of ARGUMENTS
  • Parameter value
  • Variable ascension

The scope chain

> <EC(FN),EC(G)>

Scope chain mechanism

1

  • In the private context, when we run through code, we encounter a variable, and we first look to see if it’s private in our own context,
  • If it is its own [AO], then all subsequent operations are its own and have no direct relationship with external variables

2

  • If a variable is not its own private variable, then the scope chain is used to find whether it is a variable in its parent context.
  • If found, the variables in the parent context are manipulated later

3

  • If the parent context does not have this variable, continue to find its “parent” context, all the way to the EC(G) global context location
  • If not in the global context
    • Getting the value of a variable is an error
    • The set variable value is the property set to the window

exercises

var x = [12, 23]; function fn(x) { x[0] = 100; x = [100]; x[1] = 200; console.log(x); / / [100100]} fn (x); console.log(x); [100, 23]Copy the code

console.log(a, b, c); Var a,var b,var c b = 13, c = 14; var fn = function (c) { console.log(a, b, c); //undefined 13 10 var a = b = c = 20; // c=20,b=20,var a=20 console.log(a, b, c); / / 20 20 20}; fn(10); console.log(a, b, c); / / 12 to 20 14Copy the code

If the global context has no variables

Get: error assignment: set property for window change /* /! * * EC(G) * VO(G) / GO * fn ---> 0x000 [[scope]]:EC(G) *! / var fn = function () { /! EC AO (FN) * * * * * scope chain (FN) : < EC (FN), EC (G) > * parameter assignment: - * variables: - *! / console.log(a); Uncaught ReferenceError: A is not defined a = 100; //window.a=100 }; fn(); console.log(a, window.a); / / 100 100Copy the code