r/javascript • u/saulorama • Mar 31 '17
ES2018 First Look - it changes everything
https://www.youtube.com/watch?v=s-G_RZ4RJLU45
u/n-a-a-n-u Mar 31 '17
Looking forward to it. Video was very helpful.
29
u/skitch920 Apr 01 '17
best.language.ever.
1
1
130
Mar 31 '17
Holy shit, I was getting so pissed off until I realized it was an April Fool's joke...
84
u/delventhalz Mar 31 '17 edited Apr 01 '17
"How could anyone think this was a good idea???"
- Me for a solid three minutes
16
5
u/sivadneb Apr 01 '17
As soon as he started talking about dot syntax I was all "WHY?!?" But then i was like "oh FFS."
63
u/paulirish Apr 01 '17
I didn't get to make it that far. At 0:19 in I see a tweet of... my own? And I'm saying something I never said. Apparently just two weeks ago? Methinks something is amiss!
Hats off to them with all the sneaky tweet edits.
2
15
7
3
Apr 01 '17
I'm glad that was the last thing that was said in the video for the sake of people seeing it pop up in their YT feed three weeks from now who watch it.
0
u/m3wm3wm3wm Apr 01 '17
I get pissed off about most of the new non-joke EC features, maybe this video helps you to see the irritation from my point of view.
This language had almost 0 change for 2 decades, and now it's changing as fast as the node libraries. It's like Javascript is trying to catch up with Scala.
1
u/naftoligug Apr 06 '17
As a Scala programmer, I think that's pretty accurate.
C# is busy catching up too.
0
Apr 01 '17
[deleted]
3
u/m3wm3wm3wm Apr 01 '17
If you like writing code the way you used to, with callbacks, you can still do so.
That's for me writing my code. I still need to know the new syntax when reading other people's code.
1
u/Tayk5 Apr 02 '17
The syntax changes won't be coming along as fast as the update from ES5 to ES6. It'll be incremental updates which you should be able to do hello worlds with in a few minutes and then just use them or understand them when you see them in other people's code.
Observables which are coming soon on the other hand... Takes some effort to grock.
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
fnorfunc.3
u/Disgruntled__Goat Apr 01 '17
Why? I was under the impression no one uses
functionat all now.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).
0
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
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
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 aifelse+ 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
letandconst, that means that if you wanted the same functionality in anif/elsestatement, you'd need an undefinedletvariable declaration above the if/else block, which then allows that set variable to be susceptible to (direct) mutation, since it is not aconst.-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
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 ofthisonly changes when you explicitly want it to and use thefunctionkeyword.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 * bAlso, 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
functionkeyword, but only once with arrow functions.0
Apr 01 '17
[deleted]
2
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.
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
Apr 01 '17
[deleted]
1
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
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 wanted5
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
1
1
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
thisvariable 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
0
Apr 01 '17
[deleted]
5
u/Daniel15 React FTW Apr 01 '17
const doubleBy2 = x => x * 2;Is identical to your doubleBy2 function.
2
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
thisbinding though? I feel like that's very rare.1
1
17
u/gajus0 Apr 01 '17
The transpilers for some of the ES2018 features are already available in Babel, e.g. https://www.npmjs.com/package/babel-plugin-transform-es2018-dot-syntax
16
u/shriek Apr 01 '17
Poor soul who decides to learn JavaScript for the first time and finds this video.
17
u/synackle Apr 01 '17
Damn, my heart stopped at global vars
2
0
u/blamo111 Apr 01 '17
I understood that to mean you can't reuse a variable name from a wider scope in a more local scope, which I thought was fair. You can come up with better names than that and remove ambiguity instead of shadowing.
My heart stopped at the 2nd one, keyboard shortcuts (c instead of const, f instead of function). Cause hipsters trying to Make FP Great have been promoting shorter but harder-to-read code for a while now, eg using arrow notation for no good reason. I thought "you finally really did it...you maniacs! Damn you!"
It took the 3rd one (dot notation everywhere) to allow me breathe a sigh of relief.
14
u/zergUser1 Apr 01 '17
In my room on my own I literally said outloud "WTF no it doesnt!" when he was talking about the dot syntax making everything clean and I was already thinking about a comment I was going to write to refute the changes! haha fooled me
5
u/chreestopher2 Apr 01 '17
Wow, well played, just replace all those annoying chsrscters witg a dot.... priceless
5
5
4
u/ericclemmons Apr 01 '17
Be sure to watch all the way to the end. The Flow/Typescript reference is great!
10
7
8
u/theevildjinn Apr 01 '17
Saw this post while browsing Reddit in bed at 1:30 a.m. (1st April). Thought I should go downstairs to watch it on the 50" TV screen instead of a 5" phone screen, but didn't read these comments until I was downstairs in my dressing gown waiting for the PS4 to boot up. Damn you ;-)
2
Apr 01 '17
... What?
1
u/theevildjinn Apr 02 '17
I meant that not only did they fool me, but they also made me get out of bed to be pranked as I had only read the title.
3
3
3
3
u/Smaskifa77 Apr 01 '17
Worked out it was an April fools the second the dot feature broke out. Before that I was like 'what the actual fuck are you doing'. Almost got angry.
2
2
u/u982744 Apr 01 '17
OMG I know a few JS devs that would actually like this to be real... Thank God it's not!
2
2
2
2
2
2
2
u/0x62047011 Apr 01 '17
OH GOD - I was about to comment on how effed up this was, thank goodness I read through the comments first.
2
u/lemonizer Apr 01 '17
motherfuck! I watched the entire video with a mix of anger, fear, and disbelief.
2
2
3
2
1
u/SerpentineDex Apr 01 '17
fucking hell. already ranted on twitter about it. totally got me. i guess that's what you get for browsing the webs at 4am in the morning.
1
u/zhangolve Apr 01 '17
happy april fool's day. I can't get the point that es2018 changed everything,just sugar syntax.do you?
1
1
1
-2
-2
Apr 01 '17
I was going to take a look at the changes, but then I realized that it will all be revised again in three weeks anyway, so I'll just wait till then to catch up. #javascriptEcosystem
0
Mar 31 '17
[deleted]
18
u/oculus42 Apr 01 '17
All that irritated me was April Fools videos before April.
81
u/Disgruntled__Goat Apr 01 '17
lol Americans thinking they're centre of the world
7
u/oculus42 Apr 01 '17
Fair enough. Some of the the April Fools content is already two days old cough StackOverflow cough This one seems to only be a few hours, so well within 01/04.
3
u/fogbasket Apr 01 '17
It's April fool's day, not January fool's day. Good lord.
4
u/oculus42 Apr 01 '17
Having spent most of my week dealing with international date formatting and timezone issues, I suggest we all move to ISO 8601
2
2
u/RedShift9 Apr 02 '17
Don't write stuff like that yourself, use a library. https://youtu.be/-5wpm-gesOY
2
u/oculus42 Apr 02 '17
I've been using MomentJS and Moment-Timezone.
It doesn't stop sucking, it just brings the complexity down to n from n^∞
1
2
-3
u/BenjiSponge Apr 01 '17
Well, for starters, the presenter has an American accent. Additionally, there have been a number of videos/articles today released before it was April 1 anywhere for April Fools. For finishers, who actually enjoys April Fools?
8
u/traveaston Apr 01 '17
I laughed heartily after he revealed it was an April Fools joke.
3
u/malicart Apr 01 '17
lol Americans thinking they're centre of the world
As an American I found this hilarious and sad.
7
0
Apr 01 '17
[deleted]
9
u/enigmamonkey Apr 01 '17
It is quite innovative! My code, which only I understand, has never been easier to read. However, I have this bug in my code... can you help? It's:
c f. . ... ... .... . .. . . .... . .a.. .. c.l."foo"But it always outputs
barwhen it should bebaz. Please help.0
0
0
49
u/galudwig Mar 31 '17
I was losing my shit so hard. Well played