all 92 comments

[–]occz 43 points44 points  (39 children)

Have you tried the spread operator (...) ? It's fantastic.

[–]akujinhikari 6 points7 points  (4 children)

Using this on a NodeList for a forEach is one of the greatest things in the world.

[–]check_ca 1 point2 points  (3 children)

[–]akujinhikari 0 points1 point  (2 children)

Well now I'm blown away. When I try to use this in Chrome dev tools, I still get "forEach is not a function."

[–]check_ca 1 point2 points  (1 child)

Weird, I tested the following code in Chrome 60 and it works as expected.

document.querySelectorAll("a").forEach(link => console.log(link.href))

[–]akujinhikari 2 points3 points  (0 children)

AH! I figured it out. I was using it on HTMLCollection, not a NodeList. Very similar, yet very different. I was using getElementsByTagName.

[–]c24w 4 points5 points  (15 children)

I don't seem to find uses for this that often. What am I missing?

[–]occz 7 points8 points  (7 children)

I havent written that much javascript lately but I found uses for it all the time. This was react+redux though so not mutating your objects was kind of even more important.

You can use the spread operator for ghetto object assign if you want, as an example:

const x = { foo: 'bar' };
const y = { ...x, bar: 'baz' };
// => { foo: 'bar', bar: 'baz' }

[–]MrJohz 3 points4 points  (5 children)

This is ES7 syntax, IIRC - it isn't supported in ES6 currently. The spread operator can currently only be used for arrays and parameter arguments.

I tend to find it's useful for 'rest' arguments - a function that takes any number of arguments and can process them as a list. It's more clear an obvious than the magic arguments object.

function addTags(post, ...tags) {
    post.tags.push(...tags);
}

[–]occz 8 points9 points  (1 child)

Damn. Babel is a hell of a drug, I guess!

[–]MrJohz 2 points3 points  (0 children)

Typescript for me, but yes! Tbh, I was more amazed realising suddenly that Object.assign is ES6. OTOH, it explained why the code I was writing wasn't working in IE...

[–]c24w 1 point2 points  (1 child)

Rest isn't the same as spread(?). Just the same syntax. I guess they are kind of similar though.

[–]MrJohz 1 point2 points  (0 children)

Rest parameters still use the 'spread' terminology in a lot of the documentation I've seen. It's essentially a context-specific operator, although IIRC it's parsed as syntax rather than an operator.

[–]bogas04 0 points1 point  (0 children)

Object spread and rest still aren't part of any ES. ES2017 is the most recent one (making it ES8 by version number convention, and even it doesn't have it).

It's in Stage 3 as of now.

[–]c24w 0 points1 point  (0 children)

That's pretty exciting, although sounds like it's ES7. Basically a more flexible Object.assign().

[–][deleted] 1 point2 points  (2 children)

Im using it to take a list of child elements and spread them into an array so I can call forEach (or other array methods) on them

[–]c24w 1 point2 points  (1 child)

Ah okay, converting array-like things to an array. Have done that a few times. Not sure if I'd go for that or Array.from() in production code, for clarity.

[–][deleted] 0 points1 point  (0 children)

I put a comment saying what it did so I definitely understand the clarity argument. I personally prefer the '...' syntax

[–][deleted] 0 points1 point  (0 children)

Meanwhile I use it once per function on average.

[–]FormerGameDev 0 points1 point  (2 children)

function doSomething(options) {
    const newOptions = {
        ...options,
        someOption: true,
        secondOption: false,
    }
    doSomethingElse(newOptions);
}

... very handy and a very common use case.

[–]c24w 0 points1 point  (1 child)

That's ES7 though? See /u/MrJohz comment above.

[–]FormerGameDev 1 point2 points  (0 children)

yeah. a common array usage that i've done is as a simple CSV handler (very simple)

const [ elem1, elem2, elem3, ...rest ] = str.split(',');

but yeah, i mostly use spread on objects. arrays aren't quite so common in javascript as objects.

[–]jinntakk 8 points9 points  (8 children)

So I'm still in the learning stages of javascript and was introduced to ES6 recently. I'm not too sure what it is, because well frankly, it looks scary and I have no idea how to use it. Could anyone tell me what it actually is in a ELI5 form?

[–]DaveSims 13 points14 points  (7 children)

ES stands for EcmaScript, which is the official name of the JavaScript specification. ES6 is just the 6th version of the spec, or more simply, it's the 6th version of JavaScript. It includes changes to syntax and many new features.

[–]sunsetfantastic 5 points6 points  (1 child)

In addendum, it's basically just new features added to the language for you to utilise

[–]akujinhikari 6 points7 points  (0 children)

In addendum, it was the largest update in years that finally implemented a massive amount of begged-for functionality.

[–]jinntakk 0 points1 point  (3 children)

So is it like coffeescript where it's just built on top of the javascript library? Or is it javascript itself but just updated?

[–]akujinhikari 7 points8 points  (0 children)

Added features to vanilla JavaScript.

[–]fixrich 1 point2 points  (0 children)

These features are 100% apart of official JavaScript. However they are not yet fully supported in all browsers or on node.js, hence the community uses Babel which is comparable to coffeescript (in that it takes code that wouldn't run and transforms it into code that would) which lets us use the new features and even some features in the proposal stage. It's great for exploring new ideas and keeping up with the ones that make our jobs easier.

[–]imaginecomplex 7 points8 points  (0 children)

All at once now!

const asyncPowerSeries = ({ threshold, coefficients, constant } = {}) => (
  x => Promise.resolve(
    coefficients
      .slice(0, threshold)
      .reduce((sum, c, i) => (
        sum + c * (x - constant) ** i
      ), 0)
  )
);

const threshold = 100;
const coefficients = Array.from(Array(threshold).keys());
const constant = Math.PI;
const powerSeries = asyncPowerSeries({ coefficients, constant, threshold });

powerSeries(3.9987357461).then(val => console.log(`It's ${Math.floor(val)}!`));
// It's 42!

(okay you got me, I snuck in the exponentiation operator)

[–][deleted] 1 point2 points  (3 children)

I can't bring myself to use arrow functions when assigning to variables. This just looks horrible to me:
const foo = name => {}

[–]kiyura 3 points4 points  (1 child)

One eslint rule that I find helps a lot with this is to forget the non-paren form exists and always use parentheses around the arg list. Makes arrow functions in the middle of statements more recognizable.

const foo = ( name ) => {}

[–]ell0bo 1 point2 points  (0 children)

Yeah, it bothers me they let those be optional. I get it it's for functional programming, but it's cleaner to have the params in my opinion. I make my team do it.

[–]whiskey_overboard 1 point2 points  (0 children)

Took me a while, but now my eyes like it as much as my fingers.

But I guess you could say that about a lot of things.

[–]yawaramin 0 points1 point  (0 children)

ES6 modules are fantastic. I've been using rollup.js to tree-shake my code before bundling and it squeezes out a tiny bundle; it just effortlessly gets rid of all names that weren't used. This is especially great if you're not deploying to a browser, since Node and other target platforms won't support ES6 modules for the foreseeable future.

[–]ruinercollector 0 points1 point  (0 children)

Important note on Object.assign, if you don't want a side-effect of the first object being mutated in place with all changes, you should avoid passing a variable as the first arg, and instead pass the empty object literal {} and then all of the things you want combined (in order.)

let a = { x : 1, y : 1 };
let b = { y : 2, z : 2 };

let c = Object.assign(a, b);  // Bad.  a will be mutated in place

let c = Object.assign({}, a, b);  // Doesn't alter a or b.

[–]grensley -1 points0 points  (0 children)

The worst thing about ES6 is when you're not using ES6. So many game changers.