Under global scope
This
When this is used in the global scope, it points to the global object.
Here is a detailed description of the global object:
A global object is an object that has been created before any execution context is entered;
This object only exists, its properties can be accessed anywhere in the program, and the lifecycle of the global object terminates at the moment the program exits.
The global object Initial creation phase initializes the properties of Math, String, Date, parseint, and so on, as well as additional objects created as attributes (which can point to the global object itself). For example, in the DOM, the window property of a global object can reference the global object itself.
So entering windows and This.window in the console is the same.
When calling a function
Foo ();
Here, this also points to the global object.
When a method is invoked
Test.foo ();
In this example, this will point to the test object.
When a constructor is invoked
New Foo ();
A function is used in conjunction with the keyword new when invoked, and we call it a constructor. Within the function, this points to the newly created object.
When explicitly set
function foo (A, B, c) {}//
var bar = {};
Foo.apply (Bar, [1, 2, 3]); Array would expand to the below
Foo.call (bar, 1, 2, 3);//results in a = 1, b = 2, c = 3
When you use the Function.prototype apply and call method, the value of this is explicitly set to the first parameter of the method.
Therefore, unlike the rule when calling a function, this in the example above points to bar.
Here is a description of the call and apply method:
Call method :
Syntax: Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Definition: Invokes one method of an object, replacing the current object with another object.
Apply Method :
Syntax: Apply ([Thisobj[,argarray]])
Definition: Applies one method of an object and replaces the current object with another object.
Here we should note that this cannot be used to point to the object itself when the literal declaration of an object is made. As follows:
Here, this does not point to obj,this applications that are limited to the above five scenarios.
Summarize
Although the above scenario is meaningful for most of the time, this is actually rarely useful in the second case (that is, when calling a function), which is considered to be another error on the Javascript design.
Foo.method = function () {
function test () {
//This is set to the global object
}
test ();
}
As we mentioned above, this here will point to the global object, not the Foo function.
To get a way to point Foo in test, we need to create a local variable to point to Foo inside method.
Foo.method = function () {
var = this;
function test () {
//use ' that instead of
' here}
Test ();
}
That is just a generic variable name, but it is often used to point to the external this.
And there's a more interesting place. Associated with a function alias, a method is assigned a value to a variable.
var test = someobject.methodtest;
Test ();
In the example above, test will be treated as a normal function, so depending on the second case (that is, when a function is called), the inside of this will point to the global variable instead of the someobject.
Although this is a bad decision at the beginning of this late binding, it is actually the foundation of a prototype inheritance effort.
function Foo () {}
Foo.prototype.method = function () {};
function Bar () {}
bar.prototype = Foo.prototype;
New Bar (). method ();
At this point, when method is invoked, it points to the instance object of Bar.