Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The arrow function.... The lookup of this is made exactly the same way as a regular variable search: in the outer lexical environment. #2813

Open
prad3811 opened this issue Jan 7, 2022 · 1 comment

Comments

@prad3811
Copy link

prad3811 commented Jan 7, 2022

I have tried to test this, i believe arrow function doesn't look up for 'this', in the outer lexical environment, it remembers the 'this' of the place where it is defined.
In the below code: arrow function is passed to another function and that function calls the arrow function at line //. At line // 'this' is 'window', so if arrow function looks up for this in outer lexical environment, then 'this' should be window object, not cl_Test.
Hence, i believe the statement put by you in arrow functions revisited should be changed. Please correct me if i am wrong.

    function cl_Test() {
        // let name = 'Pradeep';
        this.name = 'Pradeep';
        this.age = '31';

        this.arrowF = () => {
            console.log(`${this.name + ' ' + this.age}`);
        };
        this.normalF = function () {
            console.log(name)
        };
        this.arrowF2 = () => {
            console.log(name);
        }
    };

    function fTest(f1) {
        f1();                                                                                              // **
    };

    let o1 = new cl_Test();
    o1.name = 'Sunil';
    o1.age = '34';

    fTest(o1.arrowF);                                                                                  //*
    fTest(o1.normalF);
    fTest(o1.arrowF2);
@joachimklug
Copy link
Contributor

Hi @prad3811 ,
not sure if I fully got your argument. Maybe you can detail out to which part in the documentation you refer to.

According to the ECMA specification for ArrowFunctions following is stated:

An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function. ...

For me also your example works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants