This is the body of the function execution (by whom)

There is no correlation between who this is and where the function is created or executed.

To determine this

  • Binds a method to an event that triggers the execution of the method, and this is the element of the current operation.
  • The method is executed to see if there is a bit in front of the method, and if there is a bit in front of the method, this is whoever it is. If there is no point this is the window (in strict mode, no point this is undefined), and this of all self-executing functions is generally window
  • In constructor mode execution, this in the function body is an instance of the current class
var num = 10
var obj = {  // create a heap AF0
  num: 20
}
obj.fn = (function (num) {  // in the self-executing function AF1, the parameter num = 20
  this.num = num * 3   Num = 20*3 = 60
  num++                // num = 21
  return function (n) {  // Returns an anonymous function whose address is assumed to be BF0
    this.num += n
    num++     // the parent scope of num is AF1
    console.log(num)
  }
})(obj.num) // this is a self-executing function AF1, with num = 20
var fn = obj.fn  //fn = BF0
fn(5) BF0: num = 65; AF1: num = 22
obj.fn(10) Num = 30 in obj (AF0) and num = 23 in AF1
console.log(num, obj.num) / / 65, 30
Copy the code