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

Show parent comments

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'

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.

2

u/GBcrazy Apr 01 '17

Actually you are right, what the hell i was thinking

1

u/[deleted] Apr 01 '17

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

I can see many cases for that. Say you work in an instance of an object and would want to use multiple functions inside your class for your code, it doesn't work with arrow functions properly (or you set the this variable to something else). And i would separate those functions to make my functions lean and clean.

That would mean that if i would use arrow functions for other items, it gets mixed and makes it into more spaghetti than i would normally have when i'm not using them.

I can understand using them to minify your code but for reading and working with it, it doesn't hold any benefit whatsoever.

1

u/Wooshception Apr 01 '17

Always use the /s operator :)

/!s

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.

1

u/dupe123 Apr 01 '17

Honestly the main thing that annoys me about it is it makes the language totally inconsistent. If you want this binding you use () => but if you don't you use function()? Those are completely different. It makes me feel like I am using some patchwork language (which I am).

1

u/Daniel15 React FTW Apr 01 '17

If you want this binding you use () => but if you don't you use function()?

How often do you not want this binding though? I feel like that's very rare.