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.

1

u/fire_code !expert Apr 02 '17

I disagree on the ternary comment (? :).

It's very useful when assigning a value to a variable depending on a contextual variable/value. If you're using block-scoped variable declarations like let and const, that means that if you wanted the same functionality in an if/else statement, you'd need an undefined let variable declaration above the if/else block, which then allows that set variable to be susceptible to (direct) mutation, since it is not a const.

-1

u/Zero-Tau Apr 01 '17

To me it's more readable to use the arrow syntax simply because it's more standard: that's how most languages write it, so it's simpler and quicker to parse for me.

It's like if instead of [1,2,3] we wrote

Array 1 and 2 and 3 ArrayEnd

, then ES2016 added the square bracket syntax. People primarily used to JS might see that as shortening for the sake of shortening, and say you still have to parse it as array. But anyone who knew or used multiple languages would see that as more natural and easier to parse.

It's also simpler because the function keyword can be used in multiple configurations with different semantics but the arrow function feature always means the same thing.

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.

0

u/[deleted] Apr 01 '17

[deleted]

2

u/[deleted] Apr 01 '17

Terminology may not be correct, but in practice the first one defines the function (non-hoisted) and the second ensures it isn't anonymous with regards console logs/errors.

See: https://github.com/airbnb/javascript#functions

1

u/ajacksified Apr 01 '17

Callbacks, promise resolution, lambdas. Anywhere you're writing anonymous functions as a parameter to another function. Obviously it doesn't matter if you don't write much functional code, but I tend to.

promise.then(function (p) { dothing(p, whatever) }

promise.then(p => dothing(p, whatever);)

1

u/prium Apr 02 '17

I really like it when passing functions as arguments.

objectArray.map(object => object.property);

vs

objectArray.map(function(object) {
  return object.property;
});

Declaring functions with arrows just seems silly though.

0

u/[deleted] Apr 01 '17

[deleted]

1

u/[deleted] Apr 01 '17

Wouldn't that use a function before it is being defined?

2

u/kdesign Apr 01 '17

No, because functions are hoisted. It also makes it clear what you export at the beginning of the module so you can quickly discern between what's "private" and what's "public".

1

u/apatheorist Apr 01 '17

As kdesign said, functions are hoisted. And they are also right about the public/private distinction.

What I want to add is that eslint's no-use-before-define rule can be configured to ignore functions:

{
    "no-use-before-define": ["error", { "functions": false }]
}

It's documentation on the matter (emphasis added):

functions (boolean) - The flag which shows whether or not this rule checks function declarations. If this is true, this rule warns every reference to a function before the function declaration. Otherwise, ignores those references. Function declarations are hoisted, so it’s safe. Default is true.

-1

u/[deleted] Apr 01 '17

Because you use export anyway.