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

标签 javascript

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/

相关文章:

javascript - 水平平滑滚动

javascript - 如何切换 jQuery 中的就地编辑功能?

javascript - 使用 reangular 计算冗余

javascript - Ajax、amcharts、javascript、mongodb、django

javascript - log4javascript - 日志消息中的行号

javascript - 引用模块 javascript 中的另一个函数

javascript - [vue-test-utils] : wrapper. setChecked() 必须传递一个 bool 值

php - jQuery Masonry - 流体布局问题

javascript - react 路由器 v4 : prevent transition with custom hook instead of Prompt

javascript - 如何格式化折线图谷歌图表 Material 上的轴?