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

I think arrow functions create closures #73

Closed
jameshbush opened this issue Jun 15, 2019 · 6 comments · Fixed by #75
Closed

I think arrow functions create closures #73

jameshbush opened this issue Jun 15, 2019 · 6 comments · Fixed by #75

Comments

@jameshbush
Copy link
Contributor

The section Lexical Scoping https://ponyfoo.com/books/practical-modern-javascript/chapters/2#lexical-scoping-8zsxfqh9
claims that arrow functions don't create closures. It looks like they do. In fact, the code relies on closure of arguments in the evaluation of the returned function. I think this should say "arrow functions don't create their own lexical scope, so arguments is accessed from the closure of the enclosing function".

calc=((a)=>( (b)=>{debugger; return a+b} ))(a=1); calc(2)

Screen Shot 2019-06-15 at 15 08 04

@ljharb
Copy link
Contributor

ljharb commented Jun 15, 2019

They do create their own scope - arrow functions simply don’t create a binding for this/super/arguments, unlike all other functions, so they bubble up lexically.

@jameshbush
Copy link
Contributor Author

jameshbush commented Jun 15, 2019

Then maybe this:
"In this case, the arguments object refers to the context of the puzzle function, because arrow functions don’t create a closure. For this reason, the printed arguments will be 'a', 'b', 'c'."

Should say this:
"In this case, the arguments object isn't found in local scope, so the variable access bubbles up to the enclosing lexical context of the puzzle function, where arguments is 'a', 'b', 'c'."

(Still learning the terminology. That's the main reason I picked up this book)

@bevacqua
Copy link
Member

Could you create a PR to fix this? 🙇

@jameshbush
Copy link
Contributor Author

Will do, tomorrow :)

@jameshbush
Copy link
Contributor Author

Opened PR #75

@bevacqua
Copy link
Member

Fixed in #75

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

Successfully merging a pull request may close this issue.

3 participants