In Javascript, why is the “this” operator inconsistent?

后端 未结 8 1783
挽巷
挽巷 2020-11-22 15:04

In JavaScript, the \"this\" operator can refer to different things under different scenarios.

Typically in a method within a JavaScript \"object\", it refers to the

8条回答
  •  爱一瞬间的悲伤
    2020-11-22 15:57

    You can also use Function.Apply(thisArg, argsArray)... Where thisArg determines the value of this inside your function...the second parameter is an optional arguments array that you can also pass to your function.

    If you don't plan on using the second argument, don't pass anything to it. Internet Explorer will throw a TypeError at you if you pass null (or anything that is not an array) to function.apply()'s second argument...

    With the example code you gave it would look something like:

    YAHOO.util.Connect.asyncRequest(method, uri, callBack.Apply(this));
    

提交回复
热议问题