简体   繁体   English

什么时候应该使用try / catch而不是函数来在JavaScript中声明非全局变量?

[英]When should I use try/catch instead of functions to declare non global variables in JavaScript?

Consider following codes: 考虑以下代码:

try{
     throw undefined;
}
catch(someVariable){
    someVariable = 10;
    // do whatever you want with someVariable
    // someVariable will not be a global object at all. (without help of any function scope)
}
// someVariable is no longer valid here

Why some people use this syntax instead of functions when they don't want to declare global variables? 为什么有些人不想声明全局变量时使用这种语法而不是函数?

Additional notes: 补充笔记:

I've seen this syntax a lot, but the most important one is Google traceur 我已经看过很多这种语法,但是最重要的是Google traceur

try {
    throw undefined;
  } catch (a) {
    a = 10;
  }

that it is generated because of the following ecma script 6 syntax: 它是由于以下ecma脚本6语法而生成的:

{
    let a = 10;
}

Google traceur is a ECMA Script 6 parser on older browsers that currently have no support for new JavaScript features. Google traceur是旧版浏览器上的ECMA Script 6解析器,目前不支持新的JavaScript功能。

Well, there is a difference: Why do catch clauses have their own lexical environment? 好吧,有一个区别: 为什么catch子句具有自己的词汇环境?

try {
     throw undefined;
} catch(someVariable) {
    someVariable = 10; // someVariable will not be global
    var someOtherVariable = 5; // but any declared 'var' will be
}
// someVariable is no longer valid here
console.log(someOtherVariable); // still 5

As you can see, this is just the same behaviour as you would have with 如您所见,这与您的行为相同

{
    let someVariable = 10;
    var someOtherVariable = 5;
}
console.log(someVariable, someOtherVariable);

It's basically a hack by the Traceur transpiler to create lexical environments for single variables in EcmaScript 5. Probably Google also optimized their browser to recognize this pattern, which explains why it's quite fast on Chrome. 在EcmaScript 5中,Traceur编译器可以为单个变量创建词法环境,这基本上是黑客的行为,可能是Google也优化了其浏览器以识别这种模式,这解释了为什么它在Chrome上非常快。

In manually written code, this definitely is to be a bad practise - and without comments, no one knows what this does. 在手动编写的代码中,这绝对不是一个好习惯-没有注释,没人知道它的作用。 Use an IEFE for readability. 使用IEFE以获得可读性。

It seems a really bad practice. 这似乎是一个非常糟糕的做法。 Exceptions usually slow the execution, and making the system to raise an error an then re-use the error variable is overkill . 异常通常会减慢执行速度,并使系统引发错误然后再使用error变量是过分的 UPDATE: For me this is a bit shocking: it seems the code is faster when using a try-catch in Chrome, but slower in Firefox and faaaar slower in IE 10: Here is a test I've just created. 更新:对我来说,这有点令人震惊:在Chrome中使用try-catch时,代码看起来更快,但是在Firefox中却慢一些,而在IE 10中则慢一些:这是我刚刚创建的测试

Anyway, I think the proper way is using IIEFs like this: 无论如何,我认为正确的方法是使用这样的IIEF:

(function () {
    var someVariable=...
    ...
})();

which it's more elegant. 更优雅。 Another difference is only the variable with the error is local, any other variable created on the catch block will be global. 另一个区别是只有错误的变量是局部变量,在catch块上创建的任何其他变量都是全局变量。

You should use try/catch when it is possible that the operations will cause some error (IOException,...). 如果操作可能导致某些错误(IOException,...),则应使用try / catch。 If there is an error in try it will do what it is in catch. 如果尝试中有错误,它将执行捕获的操作。 If there is no possibility of error use function. 如果不可能出错,请使用功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM