This in JavaScript points to the problem

Source: Internet
Author: User

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 the function. Like what

function Test () {

this.x = 1;

}

The value of this will change as the function is used in different situations. But there is a general principle, that is, this refers to the object that called the function.

The use of this is discussed in detail in four scenarios.

Case one: purely function calls

This is the most common use of a function, which is a global call, so this represents the Globals object.

Take a look at the code below, which results in a 1 operation.

function Test () {

this.x = 1;

alert (this.x);

}

Test (); 1

To prove that this is a global object, I make some changes to the code:

var x = 1;

function Test () {

alert (this.x);

}

Test (); 1

The result of the operation is still 1. Change it a little bit again:

var x = 1;

function Test () {

this.x = 0;

}

Test ();

alert (x); 0

Scenario Two: Invocation as an object method

A function can also act as a method call to 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

Scenario three is called as a constructor function

The so-called constructor is to generate a new object by this function (object). 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.

Scenario Four apply Call

Apply () is a method of a function object that changes the calling object of a function, and its first parameter represents the changed object that called the function. Therefore, this is the first parameter.

var x = 0;

function Test () {

alert (this.x);

}

var o={};

o.x = 1;

O.M = test;

O.m.apply (); 0

The global object is called by default when the argument to apply () is empty. Therefore, the result of this operation is 0, which proves that this refers to the global object.

If the last line of code is modified to

O.m.apply (o); 1

The result of the operation becomes 1, which proves that this is the object o.

What does the following code output? Why?

var app={

Fn1:function () {
Console.log (this); App
},
Fn2:function () {
return function () {
Console.log (this);//window
}
},
Fn3:function () {
function fn () {
Console.log (This)//window the this and argument in the closure cannot take the outside of the function
}
FN ();
},
Fn4:function () {
return {
Fn:function () {
Console.log (this); fn function App.fn4 (). Fn (); App.fn4 () is called when an object is returned with the {fn function} {fn function}.fn () object called when the method refers to the object
}
}
},
Fn5 () {
SetTimeout (function () {
Console.log (this)//Output Window Explanation: SetTimeout the function into an asynchronous queue, according to the event loop schematic, the function in the asynchronous queue is always//on the Global environment's stack, so this points to the hosting environment
},10)
},
Fn6 () {
SetTimeout (() =>{
Console.log (this)//app object arrow function does not bind the this value in the This arrow function is related to the context in which the function is defined so point to the app
},20)
},
Fn7 () {
SetTimeout (function () {
Console.log (this)//app object???
}). bind (this), 30)
},
FN8: () =>{
SetTimeout (() =>{
Console.log (This)//{}???
},40)
}
}//app.fn1 ();//App.fn2 () ();//App.fn3 ();//App.fn4 (). Fn ();//App.fn5 ();//App.fn6 ();//App.fn7 (); App.fn8 ();

This in JavaScript points to the problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.