[英]ECMASCRIPT6 Google Tag Manager Error: Arrow function
I'm getting a compile error in Google TAG Manager (GTM) with below script:我在使用以下脚本的 Google TAG Manager (GTM) 中收到编译错误:
This language feature is only supported for ECMASCRIPT6 mode or better: arrow function.此语言功能仅支持 ECMASCRIPT6 模式或更好的模式:箭头功能。
Tried to google it but my understanding of JS doesn't seem to be enough.试图谷歌它,但我对 JS 的理解似乎还不够。 Any ideas on how to change it so Google TAG Manager can compile it?
关于如何更改它以便 Google TAG Manager 可以编译它的任何想法?
var modellist = productList.Lockers.reduce((acc, item) => {
var existItem = acc.find(({
model
}) => model === item.model);
var existItemnew = acc.find(({
modelname
}) => modelname === item.modelname);
if (!existItem) {
acc.push(item);
}
return acc;
}, []);
Change the arrow functions like (acc, item) => { ... }
to non-arrow functions like function (acc, item) { ... }
.改变像箭头功能
(acc, item) => { ... }
非箭头功能,如function (acc, item) { ... }
Object destructuring is also not allowed here, so function ({model}) { ... }
needs to become function(i) { var model = i.model; ... }
这里也不允许对象解构,所以
function ({model}) { ... }
需要变成function(i) { var model = i.model; ... }
function(i) { var model = i.model; ... }
. function(i) { var model = i.model; ... }
. The following is the fixed version.以下是固定版本。
var modellist = productList.Lockers.reduce(function (acc, item) {
var existItem = acc.find(function (i) {
return i.model === item.model
});
var existItemnew = acc.find(function (i) {
return i.modelname === item.modelname
});
if (!existItem) {
acc.push(item);
}
return acc;
}, []);
$.each(modellist, function (i, item) {
var model = modellist[i].model;
var modelname = modellist[i].modelname;
$('.filter.first .dropdown').append('<div class="' + model + '"><input type="radio" name="model" id="' + model + '" value="' + model + '" /><label>' + modelname + '</label>(<span id="modelcount"></span>)<img></div>');
});
You can convert your arrow functions into traditional functions.您可以将箭头函数转换为传统函数。 Your code would then look something like this:
您的代码将如下所示:
var modellist = productList.Lockers.reduce(
function(acc, item){
var existItem = acc.find(
function({ model }){
return model === item.model;
}
);
var existItemnew = acc.find(
function({ modelname }){
return modelname === item.modelname;
}
);
if (!existItem) { acc.push(item); }
return acc;
}, []
);
$.each(modellist, function(i, item) {
var model = modellist[i].model;
var modelname = modellist[i].modelname;
$('.filter.first .dropdown').append('<div class="' + model + '"><input type="radio" name="model" id="' + model + '" value="' + model + '" /><label>' + modelname + '</label>(<span id="modelcount"></span>)<img></div>');
});
There are a few important differences between these two types of functions in JavaScript. JavaScript 中这两种类型的函数之间有一些重要的区别。 To learn about these, check out some online resources like Arrow Functions on MDN
要了解这些,请查看一些在线资源,例如MDN 上的箭头函数
The commenters are right, the error message says exactly what's wrong: some clients don't support ES6, and yours seems to be one of them.评论者是对的,错误消息准确地说明了问题所在:某些客户端不支持 ES6,而您的似乎是其中之一。 I've written an example of how you'll have to replace it:
我写了一个你必须如何替换它的例子:
var existItem = acc.find(function(model) { model === item.model});
These two functions are (mainly) the same:这两个函数(主要)是相同的:
let myArrowFunction = (text) => console.log(text) // es6, not completely supported (newer)
let myStandardFunction = function(text) { // standard JavaScript, accepted everywhere (original)
console.log(text)
}
And you'll have to use the second version.你必须使用第二个版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.