Live data from Hacker News

Overview of JavaScript ES6 features

adrianmejia.com

231–240 of 250 posts

Re: Overview of JavaScript ES6 features

#232

Earlier quoted context omitted.

We use Babel to compile ES6/ES7/ES8 to ES5. https://babeljs.io

It's not what the article is about. The article is obviously talking about native ES6 features, otherwise it would advertise es7 features as well and advise to use transpilers.

There's no reason why someone using babel needs to use ES2016+ features, they can stick to ES2015 just like this article.

I'd actually recommend avoiding the still-not-standardized features as you'll have less/no change when it lands in browsers.

Re: Overview of JavaScript ES6 features

#233
post #144

Earlier quoted context omitted.

From the article-- let x = 'outer'; function test(inner) { if (inner) { let x = 'inner'; return x; } return x; // gets result from line 1 as expected } test(false); // outer test(true); // inner This makes it seem like let creates global variables. Why would you want to return a variable from outside the function? Doesn't that create massive overhead in terms of keeping track where variables are initially set? Easy t…

If you were to then reference 'x' from another block of code, say in another element in the case of web development, 'x' would not be a defined variable, whereas with 'var', it would be. This is mostly just a case of 'let' restricting a variable to the block it is in, and the child blocks. In your example, `let x = 'outer';` is sort of acting like a global variable, but the importance is that if another script were t…

Ahh, I get it, Thank you! In the same way var scopes to window if defined outside of a function, let scopes it to the current script block. That is very neat.

I'll read more into uses of let over var. Function level scoping a la var feels like less mental overhead, but as I read more I'm sure my opinion will change.

Re: Overview of JavaScript ES6 features

#234
post #33

Earlier quoted context omitted.

For anyone who wants to use ES6 in production, https://babeljs.io/ is amazing.

It's both amazing, and 600MB worth of dependencies. We use it for server-side code. It's high quality, and we only have a few issues with it, but I can't wait to be able to ditch is (pretty much when async/await lands in a stable node).

even once all the language features you want are in, Babel is amazing for other things such as optimizations, transforms, injecting assertions depending on environment, inlining stuff, static analysis, etc. If you use Flow its used to strip out annotations, and if you use JSX (not just in React) its great there too.

We get a lot of millage out of it even when our target platforms support all the language features we need. Your millage may vary.

Re: Overview of JavaScript ES6 features

#235

"you can start using it right now" if you don't care about IE8, IE9, IE10, and many mobile browsers and are willing to ignore 20% of your customers. e.g: http://caniuse.com/#search=let const does not have block scope in those browsers either, but will work, adding to your debugging confusion. template literals and multiline string has no IE support at all (you need edge: https://developer.mozilla.org/en-US/docs/Web/J…

That's a good point, and one of the reasons I've been reluctant to use much ES6 until it's solidly baked into > 95% of the browsers / devices / etc. (delivery platforms? whichever descriptor) that I would need to support.

A transpiler just adds another layer of complexity that it's best to avoid as much as possible, in my opinion. It's not a perfect example, but if you've ever worked with CoffeeScript and run across a bug or unexpected side effect in the final rendered code . . . that should curb some of the transpiler enthusiasm.

Some of the ES6 features look handy enough, but I'd prefer waiting to see which ones shake out as actually useful over time, versus which are just momentary novelties.

Re: Overview of JavaScript ES6 features

#236
post #198

Earlier quoted context omitted.

Arrow functions get auto-named in many browsers based on how they are used. This auto-naming is actually being somewhat standardized between browsers and means there is no difference between anonymous-style function () {} and arrow functions. Promises are more than just a wrapper around callbacks because they also standardize behavior between "stacks" of callbacks, by instead "chaining" them and creating a standard i…

Promises spreads like a virus so that all asynchronous functions eventually end up returning a Promise. They are hard to understand and it's easy to forget handling errors or return values. I think callbacks are much easier to understand and get right using named functions and closures. If you want to call everything in serial and wait between each step, why not make it synchronous instead of .then chaining ?

There are other high level operations (combinators) with Promises beyond just .then() chains: Promise.all(), Promise.race() out of the box of the spec; others from various libraries. Doing the equivalent with raw callbacks is much more complicated.

The "magic" that Promises bring to code is that they "just look serial" with .then() chains. That's part of the point of Promises and that's part of the infrastructure what powers the ability to write async/await code. Just because it looks nice and serial doesn't mean it is synchronous. (In High Functional speak, Promises are the Continuation monad, and the near isomorphism with synchronous code (especially async/await) is a wonderful product of standardizing the monad.)

Which is to say: tl;dr: Promises spreading like a virus is a feature, not a bug.

Re: Overview of JavaScript ES6 features

#237

I'm surprised by the state of const/let nowadays. The well-known good practice: use const by default; use let when it's needed. At the release of ES6, it was the way to go. But everyday I notice libraries—and some really famous— that use let everywhere in their docs, or some really influent developers from Google or Facebook who share samples of code on Twitter using let when it's not needed [1]. I don't know why. Se…

How do you feel about `const` for variables that are not reassigned but are still mutated? const array = [1, 2, 3]; array.push(100); // unexpected? I prefer to use let for bindings to objects that get mutated, even if the variable is not re-bound.

[deleted]

Re: Overview of JavaScript ES6 features

#238

Earlier quoted context omitted.

How does this work? I can't find any examples and I also don't really understand what mechanism makes your npm module work.

A tagged template literal is just a special ES6 syntax for calling a function with a specific set of arguments. You could pass a function which returns all of the arguments as an array to see what's passed in. Try the following in the console of a modern browser: function log(...args) { return args; } log `abc ${'blah'} fooo\n ${12+3} bar`; Or you could even shorten the above down to this: ((...args)=>args) `abc ${'b…

Tagged templates look interesting. It's an overlooked feature that not many people know about

Re: Overview of JavaScript ES6 features

#239

Earlier quoted context omitted.

How does this work? I can't find any examples and I also don't really understand what mechanism makes your npm module work.

A tagged template literal is just a special ES6 syntax for calling a function with a specific set of arguments. You could pass a function which returns all of the arguments as an array to see what's passed in. Try the following in the console of a modern browser: function log(...args) { return args; } log `abc ${'blah'} fooo\n ${12+3} bar`; Or you could even shorten the above down to this: ((...args)=>args) `abc ${'b…

[deleted]

Re: Overview of JavaScript ES6 features

#240
Equally spacing the points on the timeline may make for an aesthetic image, but it fails to illustrate the author's point. "As you can see, there are gaps of 10 and 6 years between the ES3, ES5, and ES6. The new model is to make small incremental changes every year." No, we can't see. In that pretty graphic, ES5->ES6 is exactly the same distance as ES6->ES7.
Post reply on HN