Saswat Arabinda
Saswat Arabinda

Reputation: 557

How to create a polyfill for spread operator

I was trying to create a polyfill for the spread operator. My objective is to create something similar to spread operator where instead of triple dots I can use triple @@@ symbols.

For example, in ES6

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

I was trying to implement similar functionalities


// Instead of triple dots, it should be triple @
console.log(sum(@@@numbers));
// expected output should be 6

I expect the output of console.log(sum(@@@numbers)); to be 6.

Upvotes: 8

Views: 12381

Answers (1)

yuval.bl
yuval.bl

Reputation: 5074

You cannot create a polyfill for spread operator.

The proper way to deal with such backward compatibility issues is to write your code in ES6, and use transpiler like babel to convert it to ES5 automatically.

Upvotes: 9

Related Questions