0

Here's the code snippet first, I'll deconstruct it in a moment.

this.argArr = [['arg1', 'arg2'], ['foo', 'bar'], ['you get ', 'the point']];

this.evalArgsFromArr = function () {
  var out = [];
  for (var _ = 0; _ < parent.argArr.length; _++) {
    out.push(someFunction(...parent.argArr[_])); // This part crashes
  }
  return out;
};

This function is part of an object, of course.

The idea is that, each item in parent.argArr should be an array, containing two arguments for someFunction(), which also handily serve as a human-readable condensation of the output. My understanding is, used on an iterable object (such as the arrays stored in parent.argArr), the spread operator outputs each individual value separately. (For example, the first run of the for loop should output someFunction('arg1', 'arg2').)

Whenever I run a file containing this in Node.js or PHP I get a SyntaxError: Unexpected token, citing the spread operator [...].

Here's the error message, if it helps: Error Message

I'm using Node 7.8.0.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
JessLovely
  • 142
  • 1
  • 12
  • Can you provide a runnable example? – Leon Aves Apr 24 '17 at 12:36
  • What version of node.js do you have ? – Vololodymyr Apr 24 '17 at 12:43
  • @JessLovely Syntax errors like unexpected tokens are not crashes. It's a parsing error that's detected before your program even starts running. Crashes are when your program compiles/parses OK, but does an illegal operation during the course of execution that wasn't forseen by the engine (trying to access a property on null, for example). I'm going to edit your post accordingly to help keep the post relevant to future visitors. Secondly, I have no problems running this so I can't reproduce the issue--probably just an outdated Node version. – ggorlen Feb 14 '21 at 20:40

2 Answers2

4

I think this is a punishment for using underscore as a variable name. But seriously, looking at your code it seems that it should work, but only if your Node interpreter is new enough to support it.

To see the support of spread operator in Node versions, see:

To use modern syntax on platforms that don't support it natively, use Babel:

Of course we cannot really test it because you didn't provide a runnable example.

But you can see this answer:

and see if you can run the example there. It uses the spread operator and if it is tested to work correctly. If it runs on your system then you should be able to use the spread operator. If it doesn't then you should really upgrade Node because there's no reason of using such an outdated version.

If all else fails then you should always be able to change:

someFunction(...array);

to:

someFunction.apply(undefined, array);

See the docs:

By the way, I'm not sure what you mean by saying that one line "likes to crash" - doesn't it always crash? It would be very strange.

eush77
  • 3,870
  • 1
  • 23
  • 30
rsp
  • 107,747
  • 29
  • 201
  • 177
  • I'm running Node 7.8.0, so it _should_ work... hm. I kind of want to avoid `apply`, since it tends to have some pretty gnarly effects on code. – JessLovely Apr 24 '17 at 13:28
  • 1
    @Papayaman1000 With Node 7.8.0 it should work. If you provided a runnable example then someone might be able to tell you what's wrong. The snippet in your question cannot be run to reproduce your error. – rsp Apr 24 '17 at 13:43
  • I'm not sure what to provide other than a dump of the whole file. Would that be appropriate/useful? – JessLovely Apr 24 '17 at 13:44
-1

You must delete "...", try:

this.argArr = [['arg1', 'arg2'], ['foo', 'bar'], ['you get ', 'the point']];

this.evalArgsFromArr = function () {
  var out = [];
  for (var _ = 0; _ < parent.argArr.length; _++) {
    out.push(someFunction(parent.argArr[_]));
  }
  return out;
};
  • How is it supposed to put array elements into individual function arguments with `...` removed? – rsp Apr 24 '17 at 12:50
  • 1
    I feel like saying "you must delete `...`" is rather unhelpful at best and wrong at worst. Especially since, the way you write it, it passes the whole array as a single argument, rather than its components as individual arguments. This is exactly why I use the spread operator in the first place. – JessLovely Apr 24 '17 at 13:33