1, as a method invocation of an object, this value is the current object
var obj={ name:"Xiao", getName () {
this. Name //xiao } }
Obj.getname ();
2, as a normal function call this point to window, when a function body, self-executing, the inside of this point to the window, if you want to change this can be var = this
var obj={ name:"Xiao", getName () { this.name // Xiao } } var fn = obj.getnamefn (); // undefined
3. When a constructor is called, it refers to the object of new
function FN (name) { this. Name=name return { name:' bb' } }var obj =new fn (' gg ') obj.name// BB This refers to the returned object
There is another situation if the constructor returns an object when it is displayed, then this point points to the returned object instead of the new object
Therefore, if the constructor returns a non-object type of data, it does not cause the above problem
4. Call, apply
Both of them are forced to change this point, they all accept two parameters, the first one is this, the second is the argument,
Apply requires that the second argument is an array of arrays or an array of classes, call is a single parameter, and calls are generally used for parameter determination, and apply is used in the case of parameter uncertainties
This in Javascript is pointing to