Hacker News new | ask | show | jobs
by shittyanalogy 4149 days ago
ECMAScript 6 is a mess

Now there's a completely new function syntax that doesn't use parens and has different scope rules

    var odds = evens.map(v => v + 1);
Enhanced object literals:

    // Computed (dynamic) property names
    [ 'prop_' + (() => 42)() ]: 42
what?? So it uses parens if there are no params, but not otherwise?

Template Strings:

    `In JavaScript this is
     not legal.`
Seriously another String delimiter?

    `Hello ${name}, how are you ${time}?`
Why aren't we just using #{} like everyone else?

    // Construct an HTTP request prefix is used to interpret the replacements and construction

    GET`http://foo.org/bar?a=${a}&b=${b}...
What??

Destructuring:

    var [a, , b] = [1,2,3];
Is that seriously just whitespace and another comma?

Splats (spreads?)

    f(...[1,2,3]) == 6
... means destructure?

This is not readable code:

    let fibonacci = {
      [Symbol.iterator]() {
        let pre = 0, cur = 1;
        return {
          next() {
            [pre, cur] = [cur, pre + cur];
            return { done: false, value: cur }
          }
        }
      }
    }
    
    for (var n of fibonacci) {
      // truncate the sequence at 1000
      if (n > 1000)
        break;
      print(n);
    }

Symbols without a literal syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Jesus Christ.

Unicode:

    "𠮷".length == 2
Awesome, still wrong.

Modules are cool. Promises are cool. Tail call optimization is cool.

This is not readable code:

    // Proxying a normal object
    var target = {};
    var handler = {
      get: function (receiver, name) {
        return `Hello, ${name}!`;
      }
    };
    
    var p = new Proxy(target, handler);
    p.world === 'Hello, world!';
ES6 is a mess. Javascript just got harder.
5 comments

     var odds = evens.map(v => v + 1);
Is about as readable as it gets (inspired by C#, Java8, CoffeeScript), as a benefit of succinct syntax sugar we get intuitive `this` binding - i.e. pit of success.

    [ 'prop_' + (() => 42)() ]: 42
> what?? So it uses parens if there are no params, but not otherwise?

Again not surprising, a leading `=>` would be a syntax error so `()` is an obvious compromise which can be naturally be extended to add args, e.g:

    evens.map((v) => v + 1)

> Seriously another String delimiter?

    `Hello ${name}, how are you ${time}?`
Yep, String interpolation is incredibly useful especially in JavaScript which does a lot of string munging - this will lead to more succinct, readable code. Should be obvious why they didn't want to break existing JS by re-using "" double-quotes.

> Why aren't we just using #{} like everyone else?

Who's everyone else (Ruby inspired langs)? Scala uses ${1 + 1} or $var shorthand (same as Groovy, Kotlin, JSP EL, Haxe), C# 6 uses {var}, Swift uses \(var) whilst Python and Java have string formats that use %(var)

    var [a, , b] = [1,2,3];
> Is that seriously just whitespace and another comma?

It's clearly ignoring matching the second element. Some languages choose to use `_` as a special ignore placeholder, JavaScript chose not to. Either way is not unintuitive with what it does so that's ok.

The other features are extremely useful if you need them, otherwise you can happily ignore them and use the subset you're comfortable with.

  > Splats (spreads?)

  >    f(...[1,2,3]) == 6

  > ... means destructure?
No, it means something very close to "apply" or "concat", depending on the usage. That example is the same as:

  f.apply(this, [1,2,3])
But it's cleaner syntax. The interesting part is that you can mix them in anywhere:

  function f(x, y, z) {
    return x + y + z;
  }
  a = [1,2];
  b = [3];

  [...a, ...b] == [].concat(a,b) == [1,2,3]
  [0, ...a, 4, 5, ...b, 6] == [].concat([0],a,[4,5],b,[6]) == [0,1,2,4,5,3,6]
  f(...a, ...b) == 6
  f(1, 2, ...b) == 6
  f(...a, 3) == 6
  f(...b, 0, ...b) == 6

> ES6 is a mess. Javascript just got harder.

Want some cheese with that whine? It got more complicated, yes. But I'm liking most of the changes, personally. A lot. Most of them are long overdue.

BTW, if you open Firefox's console, you can try out many examples. Firefox already supports tons of ES6.

    let fibonacci = {
      [Symbol.iterator]() {
        let pre = 0, cur = 1;
        return {
          next() {
            [pre, cur] = [cur, pre + cur];
            return { done: false, value: cur }
          }
        }
      }
    }
    
    for (var n of fibonacci) {
      // truncate the sequence at 1000
      if (n > 1000)
        break;
      print(n);
    }
I love how the only comment is explaining what "if (n > 1000) break;" does
Sure, you abuse the new syntax to make unreadable code, but most people will probably use it to abbreviate much of the syntax noise that plagues Javascript today.
>So it uses parens if there are no params, but not otherwise?

Params are only optional if there is a single parameter.