This usage of javascript

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 more

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.

This usage of javascript

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: 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.