this value in JavaScript anonymous function

前端 未结 4 1714
灰色年华
灰色年华 2020-11-28 11:58

Can anybody explain to me why A is true and B is false? I would have expected B to be true as well.

function MyObject() {

};

MyOb         


        
4条回答
  •  萌比男神i
    2020-11-28 12:16

    this is set based on how you call the function.
    Your anonymous function is a normal function call, so this is the global object.

    You could write (function() { ... }).call(this) to explicitly call it with your this.

提交回复
热议问题