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

Odd issue with arrow functions and this scope #733

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

Odd issue with arrow functions and this scope #733

chrisabrams opened this issue Feb 9, 2015 · 8 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@chrisabrams
Copy link

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
Copy link
Member

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

@chrisabrams
Copy link
Author

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

@eventualbuddha
Copy link
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?

@dashed
Copy link
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
Copy link
Author

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

@jamiebuilds
Copy link
Contributor

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

@sebmck
Copy link
Contributor

sebmck 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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

7 participants