已经定义了JavaScript catch参数 [英] JavaScript catch parameter already defined

查看:112
本文介绍了已经定义了JavaScript catch参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解为什么我收到以下错误,而不是如何来解决它。

I'm trying to understand why I'm getting the following error, not how to work around it.

将以下代码传递给> JSHint 产生错误'err'已定义。

Passing the following code to JSLint or JSHint yields the error 'err' is already defined.

/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: true, windows: true, bitwise: true, newcap: true, strict: true, maxerr: 50, indent: 4 */
function xyzzy() {

    "use strict";

    try { /*Step 1*/ } catch (err) { }
    try { /*Step 2*/ } catch (err) { }

}

这里显而易见的假设是 catch 表现或应该表现得像一个功能。因此,错误既不是全局变量,也不是 xyzzy 的局部变量,而是参数用于 catch 块。

The obvious assumption here is that catch behaves, or should behave, like a function. Thus, err is neither a global variable, nor a local variable to xyzzy, but a parameter for the catch block.

在浏览 ECMA-262标准,第12.14节描述了 try Statement 表示 catch 子句带有标识符,即绑定到异常。此外, catch 的语义生成规则是指参数,它将标识符作为参数传递出去

In browsing the ECMA-262 Standard, section 12.14 describing The try Statement indicates that the catch clause takes an Identifier that is bound to an exception. Additionally the semantic production rule for catch refers to a parameter that's passed calling out Identifier as an argument.

这似乎向随意的读者建议上面的代码是有效的,也许lint工具有一个bug。

This seems to suggest to the casual reader that the above code is valid and that perhaps the lint tools have a bug.

甚至 IntelliJ 最严格的JavaScript代码检查分析并未报告重新定义错误时出现问题。

Even IntelliJ's strictest JavaScript code inspection analysis doesn't report there being a problem with err being redefined.

更多的担忧,如果它是一个变量范围问题,那么人们可能会猜测错误正在流入全球空间,提出了许多其他问题,而且应该事先声明它,如下所示:

More of a concern, if it is a variable scoping concern, then one might surmise that the err is bleeding into the global space, which poses a whole host of other problems, and that instead one should declare it up front, like this:

/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: true, windows: true, bitwise: true, newcap: true, strict: true, maxerr: 50, indent: 4 */
function xyzzy() {

    "use strict";
    var err;  // DECLARE err SO IT IS CERTAINLY LOCAL

    try { /*Step 1*/ } catch (err) { }
    try { /*Step 2*/ } catch (err) { }

}

但这只会产生两个关于<$的错误c $ c> err 在每个catch语句中,使问题更严重并可能引入变量阴影

But this only results now in two errors about err at each of the catch statements, making the problem worse and potentially introducing variable shadowing.

lint工具建议每个 catch block不仅引入了它自己的词法范围,还引入了一个新变量。这可能不对。

The lint tools are suggesting that each catch block introduces not just it's own lexical scope, but a new variable as well. This can't be right.

只需制作 err1 err2 ,...安抚静态分析工具只是隐藏了症状,并没有提供更清晰的代码。

Simply making err1, err2, ... to placate the static analysis tools merely hides the symptom and doesn't contribute to cleaner code.

JavaScript Guru :这是lint工具中的一个错误,一个带有JavaScript规范的黑暗角落,还是对这里发生的事情有一个根本的误解?

JavaScript Gurus: Is this a bug in the lint tool, a dark corner with the JavaScript spec, or a fundamental misunderstanding of what's happening here?

更新:写给道格拉斯克鲁克福德,JSLint的作者,事实证明这是一个非常有效的理由。请参阅下面的答案。

推荐答案

写给 JSLint的作者Douglas Crockford,关于这个问题。

Wrote to Douglas Crockford, author of JSLint, about this issue.

那里事实证明这是一个非常有效的理由...

道格拉斯写道:


Catch变量的范围不正确,因此我建议您在每个变量中使用不同的名称。

Catch variables are not scoped correctly, so I recommend that you use a different name in each one.

如果你看一下这个类似的StackOverflow问题你请注意, PleaseStand 开始触及它。 并非所有浏览器(尤其是历史浏览器)都能正确或一致地处理作用域。

If you look at this similar StackOverflow question you'll note that PleaseStand started to touch on it. Not all browsers, especially historic ones, handle scoping correctly or consistently.

JSLint认识到您的代码可以在一个浏览器中运行,但不能在另一个,留下一个非常讨厌和微妙的bug追踪。 警告是真实的。

JSLint recognizes that your code may work in one browser, but not in another, leaving a very nasty and subtle bug to track down. The warning is real.

使用不同的名称,是的,根本不会感觉干净或简洁,因为它不是碰巧是没有遇到问题的普遍方法。

By using a different name, which, yes, doesn't feel clean or concise at all, because it isn't, happens to be the universal way of not running into the problem.

谢谢道格拉斯的澄清!神秘解决了。

这篇关于已经定义了JavaScript catch参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆