0

I am completely new to ECMA script and also JavaScript in general.

I'm trying to convert my code to work on an older version of ECMA script (I think).

The following code

Unit.prototype.getItems = function (...args) {
        let item = this.getItem.apply(this, args), items = [];
    
        if (item) {
            do {
                items.push(copyUnit(item));
            } while (item.getNext());
        }
    
        return items;
    };

Returns:

SyntaxError: missing formal parameter on the first line.

What does the ...args argument mean? What code can I replace it with to be compatible with the older version of ECMA script.

Gianmarco
  • 792
  • 2
  • 14
  • 38
Eleana
  • 1
  • What is this code going to run in? Do you know what version of ECMAScript? – evolutionxbox Jan 04 '21 at 10:50
  • 2
    Have a look at https://babeljs.io/docs/en/babel-plugin-transform-parameters. In general you can either make Babel part of your build process, or use https://babeljs.io/repl to transpile specific parts. – jonrsharpe Jan 04 '21 at 10:51
  • 2
    It's a [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters). There is no direct analogue before ES2015, but the [`arguments` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) is close. – T.J. Crowder Jan 04 '21 at 10:51
  • That said, it's a **really** old JavaScript environment that doesn't support rest parameters. Unless you need your code to run in, say, IE11, you shouldn't have to stop using them. What enviroment are you targeting? – T.J. Crowder Jan 04 '21 at 10:52

0 Answers0