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

Odd issue with arrow functions and this scope #733

Closed
chrisabrams opened this Issue Feb 9, 2015 · 8 comments

Comments

Projects
None yet
7 participants
@chrisabrams

When using the npm library thinky, I have the following snippet of code:

User.pre('save', (next) => {

    console.log("pre save event\n", this)
    next()

})

Where this will be undefined. However, if I change the arrow function to an ES5 function:

User.pre('save', function(next) {

    console.log("pre save event\n", this)
    next()

})

this is defined as expected. What would cause this to be undefined with an arrow function?

@developit

This comment has been minimized.

Show comment
Hide comment
@developit

developit Feb 9, 2015

this is lexically scoped in arrow functions. See #730.

this is lexically scoped in arrow functions. See #730.

@chrisabrams

This comment has been minimized.

Show comment
Hide comment
@chrisabrams

chrisabrams Feb 9, 2015

Ah ok...learned something new today :) Thanks @developit and @stebru

Ah ok...learned something new today :) Thanks @developit and @stebru

@chrisabrams chrisabrams closed this Feb 9, 2015

@eventualbuddha

This comment has been minimized.

Show comment
Hide comment
@eventualbuddha

eventualbuddha Feb 9, 2015

Member

@sebmck, this is the third or fourth example of this sort of question being raised. Do you think we should put it on the website or docs somewhere?

Member

eventualbuddha commented Feb 9, 2015

@sebmck, this is the third or fourth example of this sort of question being raised. Do you think we should put it on the website or docs somewhere?

@dashed

This comment has been minimized.

Show comment
Hide comment
@dashed

dashed Feb 9, 2015

Contributor

Yes please.
https://6to5.org/docs/learn-es6/#arrows

First sentence is already misleading:

Arrows are a function shorthand using the => syntax.

Contributor

dashed commented Feb 9, 2015

Yes please.
https://6to5.org/docs/learn-es6/#arrows

First sentence is already misleading:

Arrows are a function shorthand using the => syntax.

@chrisabrams

This comment has been minimized.

Show comment
Hide comment
@chrisabrams

chrisabrams Feb 9, 2015

That is exactly how I was mislead. I did not realize that the => behaves similarly to the => in CoffeeScript.

That is exactly how I was mislead. I did not realize that the => behaves similarly to the => in CoffeeScript.

@jamiebuilds

This comment has been minimized.

Show comment
Hide comment
Member

jamiebuilds commented Feb 9, 2015

Open an issue in https://github.com/6to5/6to5.github.io please

@kittens

This comment has been minimized.

Show comment
Hide comment
@kittens

kittens Feb 9, 2015

Member

@eventualbuddha Not sure if there's anything else that can be done since in all the examples where arrow functions are shown they're always presented with an automatic lexical scope binding.

Member

kittens commented Feb 9, 2015

@eventualbuddha Not sure if there's anything else that can be done since in all the examples where arrow functions are shown they're always presented with an automatic lexical scope binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment