The this in JS is so magical that I get dizzy.

Source: Internet
Author: User

A common judgment of this:

1. function pre-compilation process This -> Window

2. global scope in this -> Window can change when the function runs this point

4.obj.func (); the This in func () points to obj(that is, who calls the function and this in the function points to whom)

Second, the common use of this:

1. In the custom constructor:


function A () {        this. Name = ' a ';          this. Age =;    }

2. In the function definition of the object, this point points to the object, which can be called within the function by this object's properties.


var obj = {        ' lyl ',        function  () {            Console.log (this // lyl         }    }

3. Define the method on the prototype chain:


function () {        //  This refers to an array that invokes the method    }

4. When JS defines an event:


function () {        //  This points to the DOM object,  $Li this        . Style.color = ' red ';    }

Three, Strict mode:

ES5 in non-strict mode, this default points to window

ES5 Strict mode, this initial default is undefined, you need to assign a value, and why is the assignment, assignment is Undefined/null is no exception

The this in JS is so magical that I get dizzy.

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.