1. Purely function calls, this represents global objects
var x = 1; function Test () { var x = ' er ha '; Console.log (this. x); // 1 }test ();
2, as a member method of an object, this represents the object
x = ' er ha ' ;
function Test () { console.log (this. x); //1} var obj = { 1, Fn:test}obj.fn ();
3, as a constructor call, this represents the new object that is generated by the ' new ' keyword
x = ' er ha ' ;
function Test () {
this. x = 1;} var New test (); Console.log (obj.x) // 1
4. When using the call () method or Apply (), the first parameter of this method is the object that called the function after the change, this refers to the first parameter, when the argument is empty, the global object is called by default
var x = 1; function Test () {Console.log (this. x);} var obj = { ' er ha ', fn:test};obj.fn.apply (); // 1
The This keyword in JavaScript points to a problem