The JS this function is detailed
This article on the JavaScript this function in detail, and knowledge of the summary of the collation, thoroughly understand the JS this function how to use.
This code function is called, the
.1 direct Call function then this points to the Window object
.2 class call point to this class
.3 method. Apply (OBG); At this point the inside of this method points to obj without arguments pointing to window
.4 function as a constructor, the internal this points to the newly created object
Summary of a-class direct structure
This is a keyword in the JavaScript language. It represents a function runtime, automatically generated an internal object, can only be used inside the function, the following four cases, detailed discussion of the use of this, interested friends can understand the next
This is a keyword in the JavaScript language.
It represents an internal object that is automatically generated when the function is run, and can only be used inside a function. Like what
function test () {
this.x = 1;
}
The value of this will change as the function is used differently. But there is a general principle, and that is this refers to the object that invokes the function.
Here are four different scenarios for a detailed discussion of this usage.
Case one: pure function call
This is the most common use of a function, which is a global call, so this represents the globally object.
Take a look at the code below and it runs 1.
function test () {
this.x = 1;
alert (this.x);
test ();//1
To prove that this is the global object, I make some changes to the code:
var x = 1;
function test () {
alert (this.x);
test ();//1
The results of the operation are still 1. Change again:
var x = 1;
function test () {
this.x = 0;
test ();
alert (x); 0
Case two: Call as an object method
A function can also be invoked as a method of an object, at which point this is the ancestor object.
function test () {
alert (this.x);
var o = {};
o.x = 1;
O.M = test;
O.M (); 1
Case three is called as a constructor function
The so-called constructor, is to generate a new object by using this function. At this point, this is the new object.
function test () {
this.x = 1;
var o = new Test ();
alert (o.x); 1
The run result is 1. To show that this is not a global object, I make some changes to the code:
var x = 2;
function test () {
this.x = 1;
var o = new Test ();
alert (x); 2
The run result is 2, indicating that the value of global variable x does not change at all.
Condition four Apply Call
Apply () is a method of a function object that changes the calling object of the function, and its first argument represents the object that called the function after the change. So, this is the first argument.
var x = 0;
function test () {
alert (this.x);
}
var o={};
o.x = 1;
O.M = test;
O.m.apply (); 0
When the parameter to apply () is empty, the global object is called by default. Thus, the result of this operation is 0, proving that this refers to a global object.
If the last line of code is modified to
The operation turned out to be 1, proving that this is the object o
Thank you for reading, I hope to help you, thank you for your support for this site!