Js callback this problem

Why is the value of this tag 1 and this tag 2 different when the second callback is made by the two calls
?

 function a (val, callback) {
      callback.call(a,val,callback)
    }
    a(1,function b (val ,callback) {
        console.log(this, "1")
        console.log(val)
        console.log(callback)
        callback(2,function c(val, callback){
        console.log(this)
        console.log(val)
        console.log(callback)
        console.log("over")
      })
    })
    console.log("------------")
    a(1,function b () {
        console.log(this, "2")
        console.log(arguments[0])
        console.log(arguments[1])
        arguments[1](2,function c(){
        console.log(this)
        console.log(arguments[0])
        console.log(arguments[1])
        console.log("over")
      })
    })

clipboard.png


the first step is to look at the definition of function a

.

clipboard.png

observe that there are three callback, in the picture above callback,

  1. callback in the parameters of function a
  2. bind the callback, executed on an and the corresponding this when it executes is the function a
  3. the callback,3 number callback in the parameters of callback execution on
  4. 2 does not specify who the this is bound to, and determines
  5. at execution time.

the second step is to simplify your experimental code and highlight this

  

Sorry I can't go on. I just read the answers of the first two, and that's about it.

Menu