3

我需要停止我的脚本。我不想写更多的代码,if(...) return false因为我会多次使用它。

我的英语很差。这是我在这个网站上的第一个问题。:)

4

2 回答 2

4

你可以简单地写return false;来这样做。

于 2013-01-09T06:22:13.497 回答
0

为了退出脚本的所有范围,您可以抛出异常并在脚本的外部范围内捕获它;)这要求您调整异常处理。

请注意:最好以这种方式编写程序,以下不是必需的!

function ExitException() {}

try { //Do your script-stuff here

  Foo();

  //and do this at any place where you wish to exit:
  throw new ExitException;
}
catch (e) {
  if (e instanceof ExitException) //This is an Exit. All fine.
    console.log("Exit");
  else //Pass Exception to the Browser
    throw e;
}
于 2013-01-09T06:20:21.950 回答