gpt4 book ai didi

javascript - javascript1.7 赋值中左侧无效

转载 作者:行者123 更新时间:2023-12-02 19:21:42 27 4
gpt4 key购买 nike

GoogleChrome 中的 JavaScript1.7 错误

示例:

var x, y;
try {
[x, y] = ["1", "2"];
document.write(x+": "+y);
} catch(e){
console.log(e);
}

捕获错误:JavaScript1.7 赋值中左侧无效

我实际上需要的是测试浏览器是否支持javascript1.7

我需要的是实际测试浏览器是否支持javascript1.7但不使用这种代码:

js版本=1.7;

此代码除了被 w3c 无效外,还可以使用全局变量,这可能是一件非常糟糕的事情(可能导致冲突和其他问题)

我尝试使用以下代码(但在“strict”中出现错误)

var i=0;
try{
yield i;
} catch(ee) {
document.write(ee);
}

这就是我需要做的,测试浏览器是否支持 javascript1.7。

谢谢。

最佳答案

尝试

var isESNext;
try {
eval('(function () { var x, y; [x, y] = [1, 2]; })()');
isESNext = true;
} catch (ex) {
isESNext = false;
}

这使用 eval 来解析使用 ES.next 的一段代码。模式分解,但不是有效的 EcmaScript 5。声明和分配 isESNext 的其余代码是有效的 EcmaScript 3 代码,但应该在所有浏览器上运行。

您遇到的问题是由于 Chapter 16 :

16 Errors

An early error is an error that can be detected and reported prior to the evaluation of any construct in the Program containing the error. An implementation must report early errors in a Program prior to the first evaluation of that Program.

...

An implementation must treat any instance of the following kinds of errors as an early error:

  • Any syntax error.

但是上面的代码片段解决了早期的错误,因为

Early errors in eval code are reported at the time eval is called

关于javascript - javascript1.7 赋值中左侧无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469240/

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