node-es6-examples icon indicating copy to clipboard operation
node-es6-examples copied to clipboard

Array pattern destructuring not actually supported?

Open ninegrid opened this issue 10 years ago • 5 comments

When demonstrating generators:

function* fibonacci() {
  let a = 0, b = 1;

  while(true) {
    yield a;
    // this syntax doesnt appear to be actually supported
    [a, b] = [b, a + b];
  }
}

ReferenceError: Invalid left-hand side in assignment

I'm using node v0.11.13 with the --harmony flag.

ninegrid avatar Jun 21 '14 05:06 ninegrid

Sorry, I took another look at the document and saw at the bottom:

"We look forward for tasty syntax sugar yet to come, such as rest and spread operators, destructuring "...

ninegrid avatar Jun 22 '14 04:06 ninegrid

No no, this is good. Thanks for posting. :) All examples should run in the latest Node.

JustinDrake avatar Jun 22 '14 11:06 JustinDrake

No problem then, I've been playing with harmony a lot lately and this resource is excellent. Though this feature is not currently present, it would appear that it is inbound in v8

ninegrid avatar Jun 26 '14 06:06 ninegrid

Hi Justin, there is a mistake in given Fibonacci example. If n is 0 it is 0 (e.g. Ron Knott's Fib table).

I share my code at jsbin.com for ES5

// iterative
function fibonacci (n) {
    var i = 0,
        temp = [i, i + 1];

    for (i = 2; i <= n; i += 1) {        
        temp[i] = temp[i - 1] + temp[i - 2];
    }  

    return (temp[n]);
}

// recursive one liner (very slow if n > 50 ! & bad to read)
function f (n) {
  return ((n >= 2) ? f (n - 1) + f (n - 2) : n);
}

ronny-springer avatar Nov 26 '14 15:11 ronny-springer

@ronny-springer perhaps you should create a seperate issue for that so that it gets noticed. This issue has to do with destructuring syntax in general and not the fibonacci example per se...

ninegrid avatar Dec 24 '14 20:12 ninegrid