javascript - 从 ARROW 函数 (es6) 到 ES5
问题描述
在我的 angularjs 应用程序中,我使用 esprima.js 验证和 gulp。现在,esprima 给我这两个 js 的错误。
internal/streams/legacy.js:59
throw er; // Unhandled stream error in pipe.
^
错误:第 38843 行:意外的标识符
这是第一块
var filteredCampaignItems = campaignItems.filter((thing, index, self) => index === self.findIndex((t) => (
t.expectedAdvertisementDisplayCount === thing.expectedAdvertisementDisplayCount && t.smartId === thing.smartId
))
)
第二个是求和的过滤器
app.filter('sumProduct', function() {
return function (input) {
var i = input instanceof Array ? input.length : 0;
var a = arguments.length;
if (i === 0)
return i;
var total = 0;
for(var x of input){
var duration = parseFloat(x.meta_duration);
if(isNaN(duration)){
throw 'filter sumProduct can count only numeric values';
}
total += duration;
}
return total;
}
});
Esprima 版本是"version": "4.0.0"
,我发现我需要获取版本的instanbul 依赖"istanbul": "^1.0.0-alpha.2"
,我手动更改为版本1.0.0-alpha.2
并调用npm i esprima
。我这样做,但同样的错误被抛出。
有人知道如何解决这个问题吗?
解决方案
我推荐使用 BabelJS ( https://babeljs.io/ )。
Babel 是一个编译器,可以将每个“新”功能转换为“旧”ES5 代码(目标级别是可配置的,但我想在大多数情况下 ES5 都可以)
他们甚至有一个在线工具可以现场试用(https://babeljs.io/repl/)
推荐阅读
- java - 如何在 Java 8 时间库中获取 3 个字符的时区显示名称?
- javascript - 在 react native 的渲染中使用变量
- android - 如何以编程方式将片段添加到后台堆栈?
- javascript - 图片不完整。如何回调和加载图像?在 setInterval 内绘制函数
- android - 我想从这个收到的 JSON 响应中提取标签的内容。怎么做?
- excel - 将用户表单中的 10 个条目添加到工作表
- twitter-bootstrap - 引导卡相同宽度
- java - 检测类后没有触发Java断点?
- javascript - 定位每个 div(相同的类名)并将其内容复制到 Javascript 中的另一个 div
- bash - 如何从文件中提取未知数量的行并为每个行生成一个新文件?