gpt4 book ai didi

javascript - 未捕获的 SyntaxError : Unexpected eval or arguments in strict mode: window. gtag = (arguments) => dataLayer.push(arguments);

转载 作者:行者123 更新时间:2023-11-30 14:48:29 31 4
gpt4 key购买 nike

我有以下代码:

class MetricGoogleGateway extends AMetricGateway{
constructor(id, name, token) {
super(id, name);
this.token = token;
}

configure() {
if(!window.dataLayer && !window.gtag)
{
window.dataLayer = window.dataLayer || [];
window.gtag = (arguments) => dataLayer.push(arguments);
}

gtag('js', new Date());
gtag('config', 'UA-113071675-1');
}
}

当浏览器加载文件时,出现以下错误:

Uncaught SyntaxError: Unexpected eval or arguments in strict mode

但是如果我在 chrome 控制台中运行以下几行,一切正常:

window.dataLayer = window.dataLayer || [];
window.gtag = (arguments) => dataLayer.push(arguments);
window.gtag(1);
window.gtag(2);
console.log(window.dataLayer)
-> console.log result: [1,2]

注意:

我注意到,如果我更改行:

window.gtag = (arguments) => dataLayer.push(arguments);

对于行:

window.gtag = function(arguments) { dataLayer.push(arguments) }

同样的错误

最佳答案

您的代码位于 class 主体中,该主体在 strict mode 中运行(就像您所有的代码一样),与粘贴到控制台中的代码段不同。

名称argumentseval 是特殊的,禁止用作标识符。不要使用它们。只需编写 args,或者更好的是一些真正有意义的东西,比如 layer

window.gtag = (layer) => dataLayer.push(layer);

关于javascript - 未捕获的 SyntaxError : Unexpected eval or arguments in strict mode: window. gtag = (arguments) => dataLayer.push(arguments);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519484/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com