The scope of the ARROW function __ function

Source: Internet
Author: User
Scope of the Arrow function

The arrow function scope is bound to the context of the parent.
Test Cases:

var obj = {
    id:1, show
    : () => {
       console.log (this.id)
    }
}
obj.show ()

Output: undefined
In this case the show function is the arrow function, so this is bound to this in the parent, which is a top-level object in the browser, this points to the Window object, and the window does not have a property ID, so the output undefined

Test Case 2:

var obj = {
    id:1,
    show:function () {return
        () => {
            console.log (this.id)
}}}
obj.show () ()

Output: 1
In this case, the arrow function is an anonymous function whose parent scope is the show function, so the arrow function binds to the scope of the show function, which points to the object that calls the show function, that is, the id attribute in the Obj,obj, and the value is 1, so the output 1

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.