是否有像PHP的exit()这样的JavaScript函数?

时间:2013-01-09 06:17:22

标签: javascript exit

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

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

2 个答案:

答案 0 :(得分:4)

您只需编写return false;即可。

答案 1 :(得分: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;
}