Learn the This keyword in JS instant execution function

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.

  function11

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

 var x = 1;  function  Test () {alert (this//

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

  var x = 1;  function  Test () {  this. 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 == 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; }  varnew//

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; }  varnew//

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== 1=//

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 ********* Further, save as HTML file open **********
<Scripttype= "Text/javascript">(function() {x= A; functionTest () { This. x= 1; } varo=Newtest ();//var o =test (); difference: There is a new pop-up 22, no new pops up 1alert (x);} ())</Script>

Learn the This keyword in JS instant execution function

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.