r/javascript • • Mar 31 '17

ES2018 First Look - it changes everything

https://www.youtube.com/watch?v=s-G_RZ4RJLU
464 Upvotes

120 comments sorted by

View all comments

25

u/azangru Apr 01 '17

I actually listened to a fresh interview with Brendan Eich yesterday, where he said that the keyword function he chose when he was prototyping JavaScript turned out to be a bit on the longish side for the JavaScript parser, which negatively affected its speed; so when the video mentioned the shortcut for the function keyword, it sounded a little too real.

19

u/delventhalz Apr 01 '17

To be honest, I wouldn't mind being able to use fn or func.

3

u/Disgruntled__Goat Apr 01 '17

Why? I was under the impression no one uses function at all now.

0

u/[deleted] Apr 01 '17

[deleted]

20

u/ajacksified Apr 01 '17

() => 'welcome to the future'

7

u/azangru Apr 01 '17

Some people dislike anonymous functions (cough — Kyle Simpson — cough)

Some people like function hoisting.

¯_ (ツ) _/¯

6

u/[deleted] Apr 01 '17

If we're just writing a normal, ordinary function, what benefit does

const myFunction = () => { doStuff; }

have over

function myFunction() { doStuff; }

assuming the function doesn't invoke this?

To put it another way - other than using shiny new features for the sake of using shiny new features (which is, honestly, the sort of thing I would do), why change how you write ordinary functions?

3

u/[deleted] Apr 01 '17

Yeah i'm baffled by that too. It doesn't make it more readable because you have to interpret the () => as being a function. And when you have some weird nesting going on because your application isn't only going to display "Hello world", it can become quite a mess and hard to read.

I can understand why a uglifier could translate it, because it simply has less characters, but i'm already using a tool that does that for me for release. But i don't know why one would want to use that for a developing state. Btw, you can also have:

const myFunction = function() { doStuff; }

Since you set one to a variable and the other is not.

Its the same argument i had about the whole shortening and dot syntax while watching. It only makes things shorter, not readable. Its also the reason why i never shorten a if else with the ? : syntax. It doesn't matter as i can let my compiler shorten it for me and i already have a ifelse + tab shortcut on my editor. Shortening for the sake of shortening is never going to make your code more readable. And if you do it to write less code yourself, you are doing it wrong. I get why a compiler would benefit from it, but a human should not.

→ More replies (2)

2

u/[deleted] Apr 01 '17 edited Apr 01 '17

assuming the function doesn't invoke this?

That's the crucial detail. I prefer arrow functions because they leave no ambiguity regards this; the context of this only changes when you explicitly want it to and use the function keyword.

Also, I like how terse they are and how they allow shorthand returns. Compare these:

function multiply (a, b) { return a * b }
// or (see below)
const multiply = function multiply (a, b) { return a * b }

Or:

const multiply = (a, b) => a * b

Also, consider hoisting. If you don't want hoisting (I don't generally) then you'll have to define the function name twice if using the function keyword, but only once with arrow functions.

→ More replies (3)
→ More replies (2)
→ More replies (6)

14

u/GBcrazy Apr 01 '17

Well you can't really reuse arrow functions unless you put it into an object. Also it always binds this...there are some scenarios where this arguably isn't wanted

5

u/ajacksified Apr 01 '17 edited Apr 01 '17

I feel like my current 0 karma is the number of people who understand sarcasm

Edit: and anyways if you don't want to bind this, don't use an arrow function

Edit2: can't reuse functions? What are you even talking about, that makes no sense

0

u/[deleted] Apr 01 '17

[deleted]

5

u/Daniel15 React FTW Apr 01 '17
const doubleBy2 = x => x * 2;

Is identical to your doubleBy2 function.

→ More replies (1)
→ More replies (3)

1

u/dupe123 Apr 01 '17

What I don't get is why haven't they written a skinny arrow function that doesn't rebind this? Is there some technical reason that this can't be done?

3

u/Daniel15 React FTW Apr 01 '17

Probably because other languages that have the same arrow function syntax (C#, Hack) don't have one. Yes yes, I know that CoffeeScript does.

That and the use cases are very narrow. There's not really a reason to complicate the syntax if it's not necessary. If you really want to maintain this, you can just use regular function syntax.

→ More replies (2)

1

u/bugeats Apr 01 '17

Arrow functions are not the same thing god damnit.

1

u/Disgruntled__Goat Apr 01 '17

It wasn't a totally serious comment.

2

u/inu-no-policemen Apr 01 '17
function foo() {...}

is much nicer than:

const foo = () => {...};

ESLint and TypeScript's compiler will complain about redeclaring functions. So, there really is no benefit whatsoever in practice. The const option is longer, less easy to scan, and you have to pseudo-declare your functions in the right order.

The only benefit is that it shows newbies that JS has first-class functions. I.e. that you can assign them to variables. Personally, I really don't care for that. First-class aren't that special anymore.

I'll always prefer the declarative option over an imperative one.

1

u/Disgruntled__Goat Apr 01 '17

The only benefit is that it shows newbies that JS has first-class functions. I.e. that you can assign them to variables.

You don't need the short function syntax for that.

const foo = function() { ... };

2

u/inu-no-policemen Apr 01 '17

Meant that it's the only benefit of using an assignment instead of a declaration.

1

u/delventhalz Apr 01 '17

That's fair. I mostly do when I write JS. Been doing a lot of Python lately (gag me).