addyosmani / es6-equivalents-in-es5

WIP - ES6 Equivalents In ES5

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some question about the example of rest parameters.

CoderK opened this issue · comments

I think that the es5 example code in the rest parameters section can be shortened.

AS is :

"use strict";

function f(x) {
  var y = [];

  for (var _key = 1; _key < arguments.length; _key++) {
    y[_key - 1] = arguments[_key];
  }

  // y is an Array
  return x * y.length;
}
console.log(f(3, "hello", true) == 6);
// -> true

To be :

function f(x){
    return x * (arguments.length - 1);
}

console.log(f(3, "hello", true) == 6);

Did you write some complicated code deliberately to let es6 syntax be emphasized?

My take,

Your example, @CoderK, just gets to the same result value but doesn't show how things work "inside" ES6.

When the comment says "y is an array" it's showing how ES6 behaves. Every argument past the first one becomes (in this specific example, using (x, ...y)) part of an array.

The example seems to have changed since the creation of this issue but the point remains. At the moment every argument past the first is part of an array you're in pretty much the same place as you would be with ...y in ES6.

The example now uses var y = []; y.push.apply(y, arguments) && y.shift(); Another way to make y a true array of all arguments past the first.

Wow, I repeated myself a lot. =P
I'll stop.

Let me know if I didn't made myself clear enough, or at all. =P
Thanks.

Wow, I got it.
Thanks for your reply.

I'm going to close this issue.