A simple summary of the question about this point in JS

Source: Internet
Author: User
1: Under global scope the This object points to the Window object

For example:

The results are as follows:

2: Under Function scope

In non-strict mode: The point of this is still the Window object

For example:

The results are as follows:

In strict mode:

The point of this is undefined

For example:

The results are as follows:

So inside the function, in strict mode, this point or value is undefined

3: Inside the object

The point of this is the current object

For example:

The results are as follows:


There is a classic question about this point:

Please see:

In non-strict mode, the results are as follows:

If in strict mode, for example:

The results are as follows:

So how do you get the function fn to use the current obj object, the analogy is to output the value of name in FN, how to write?

The first method:

The second solution: (using the arrow function can be very good solution)


A simple summary of the question about this point in JS

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.