3 ms·
> Arrow functions are a popular feature in modern JavaScript - they allow us to write functional code in a concise way. > Rust has something similar and they a
by dgb23 6y ago
> Arrow functions are a popular feature in modern JavaScript - they allow us to write functional code in a concise way.
> Rust has something similar and they are called “Closures”. The name might be a bit confusing (...)
This is in turn confusing to me!
I can't imagine a world where you know JS and especially "functional" JS, but haven't heard of the term "closure".
"Arrow functions" are just sugar. They don't have any special meaning. All functions in JS, regardless of whether they are declared, assigned, returned etc., are closures and capture their environment.
Similarly a normally declared function in Rust are first class values as well and for example can be passed into higher order functions.
Giving the benefit of the doubt, I assume that the author knows this and just wanted to introduce the term in a beginner friendly manner. But I think it would be less confusing to just use the term and link to an official documentation:
Rust: https://doc.rust-lang.org/book/ch13-01-closures.html https://doc.rust-lang.org/book/ch13-01-closures.html
JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures https://developer.mozilla.org/en-US/docs/Web/JavaScript/Clos...
- esperent 6y ago> "Arrow functions" are just sugar This statement is incorrect. There are important differences between arrow functions and regular function. > An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- dgb23 6y agoI include this in sugar as in: You can refactor an arrow function into a specific regular function but you usually end up with something more verbose. Or in other words you can express all things "arrow" before it existed. Which is exactly what we did before they were adopted. And we still in fact do, just automated by transpilers like Babel[0]. But more importantly, in the context of the article: all functions in JS are closures, regardless of how you write them. [0] https://babeljs.io/docs/en/babel-plugin-transform-arrow-functions https://babeljs.io/docs/en/babel-plugin-transform-arrow-func...
- esperent 6y ago> I include this in sugar as in: Fair enough, but you are probably alone in doing so.
- FranceBacce 6y agoWell you couldn't resolve "this" lexically without some closure tricks like that = this , so sure, you could do everything you do with arrows without them, but not with one function alone