2

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 ...?

4
  • 4
    have you heard of babel Commented Aug 7, 2018 at 3:37
  • 1
    isn't it (almost) equivalent to alias.writeDialogue = writeDialogue? Commented Aug 7, 2018 at 4:01
  • 1
    @appleapple I've used that statement earlier on in the programme, so you'd've thought that I'd think to use it now, and that solution, in this case, is exactly what I needed. Thank you for pointing out my idiocy! EDIT: Ah - but will alias.x = x also return what x returns? EDIT: Yes it will. Commented Aug 7, 2018 at 11:17
  • I'm glad to help :) Commented Aug 7, 2018 at 13:08

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.