JavaScript Tutorial: This use instance

Source: Internet
Author: User
Tags object constructor

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


O.m.apply (o); 1


The run turns into 1, proving that this is the object o.



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.