如何为点差运算符创建一个polyfill [英] How to create a polyfill for spread operator

查看:73
本文介绍了如何为点差运算符创建一个polyfill的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为点差运算符创建一个polyfill。我的目标是创建类似于散布运算符的东西,在其中我可以使用三个@@@符号代替三个点。

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.

例如,在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

我希望输出 console.log(sum(@@@ numbers)); 6

推荐答案

无法创建传播的polyfill 运算符。

处理此类向后兼容性问题的正确方法是在ES6中编写代码,并使用翻译器,例如 babel 自动将其转换为ES5。

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.

这篇关于如何为点差运算符创建一个polyfill的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆