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
.
Syntax: var variablename1 = [... value]; In the above syntax, … is spread operator which will target all values in particular variable.
The fundamental idea of the object spread operator is to create a new plain object using the own properties of an existing object.
Spread properties is a part of ECMAScript 2018 which is not supported by IE. You can use Babel to transpile it.
One of the alternatives to the spread operator is the Object. assign function. Here is the same function using the object. assign function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With