Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 446
Saswat Arabinda Avatar asked Jul 18 '19 10:07

Saswat Arabinda


People also ask

How do you write a spread operator?

Syntax: var variablename1 = [... value]; In the above syntax, … is spread operator which will target all values in particular variable.

Does spread operator creates new object?

The fundamental idea of the object spread operator is to create a new plain object using the own properties of an existing object.

Does IE 11 support spread operator?

Spread properties is a part of ECMAScript 2018 which is not supported by IE. You can use Babel to transpile it.

What can I use instead of a spread operator?

One of the alternatives to the spread operator is the Object. assign function. Here is the same function using the object. assign function.


1 Answers

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.

like image 57
yuval.bl Avatar answered Oct 19 '22 13:10

yuval.bl