JavaScript function scope and this point to the detailed

Source: Internet
Author: User

First, to say a simple concept-variable promotion:

By means of the function+ function name, the declared function can be called anywhere in the code;

  

In the way that variables are defined by VAR, the declared function must be called after the declaration because the value of the variable is undefined when called before the variable definition;

  

Second, to say a particularly important concept-function at the time of definition (not at the time of the call), has determined the function of the scope of the internal free variables;

The above concept is particularly important for understanding this point!!!

1. First look at an example:

    

Bar (FN), the FN function executes, and its this point is undoubtedly pointing to the window, but Bar.call ({},fn), changing the bar function's this point, the FN function's this point is not changed. This is because the this point of the FN function, as defined by the FN, is pointed to the window, and is not changed because the this point of the called function bar changes;

2. This of the directly defined function points to window:

As the above example shows, the function is pointed to and determined to point to window when defined, although the this point of the outer function is changed, but the this of the inner function still points to window;

    

The inner layer function is defined inside the outer function, and its this point is still the window;

3. This is a pointer to the instantiated object through the object defined by the function:

    

4. In a function defined inside the constructor, this points to the constructor:

    

5. The pointer to this in the arrow function is consistent with this point of the outer function:

    

It is known that the use of the arrow function, equivalent to the outer function of this point is bound to the inner layer of the ordinary function;

JavaScript function scope and this point to the detailed

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