snazzybouche
snazzybouche

Reputation: 2427

Replacing the spread operator?

I have the following function.

alias.writeDialogue = function() {
    return writeDialogue(...arguments);
};

I wish to support IE, which doesn't support the spread operator. With what should I replace ...?

Upvotes: 2

Views: 1084

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370689

Use apply to transform an array of arguments into an argument list:

return writeDialogue.apply(undefined, arguments);

But it would be better to integrate Babel into your build process, so that you can write with modern syntax and have it transpiled into ES5-compatible syntax automatically. For example

https://babeljs.io/repl/

Plug in

alias = {
  writeDialogue: function() {
    return writeDialogue(...arguments);
  }
}

and you get

"use strict";

alias = {
  writeDialogue: function (_writeDialogue) {
    function writeDialogue() {
      return _writeDialogue.apply(this, arguments);
    }

    writeDialogue.toString = function () {
      return _writeDialogue.toString();
    };

    return writeDialogue;
  }(function () {
    return writeDialogue.apply(undefined, arguments);
  })
};

Babel will also automatically transpile arrow functions, const and let, async/await (with RegeneratorRuntime), and so on. It's a must-have.

Upvotes: 4

Related Questions