spu*_*dly 198
throw new Error("my error message");
Run Code Online (Sandbox Code Playgroud)
Eli*_*rey 30
break如果标记它,则只能使用块范围.例如:
myBlock: {
var a = 0;
break myBlock;
a = 1; // this is never run
};
a === 0;
Run Code Online (Sandbox Code Playgroud)
您不能在范围内的函数内中断块范围.这意味着你无法做到这样的事情:
foo: { // this doesn't work
(function() {
break foo;
}());
}
Run Code Online (Sandbox Code Playgroud)
您可以使用函数执行类似的操作:
function myFunction() {myFunction:{
// you can now use break myFunction; instead of return;
}}
Run Code Online (Sandbox Code Playgroud)
the*_*mhz 22
您只需使用该return;示例即可
$(document).ready(function () {
alert(1);
return;
alert(2);
alert(3);
alert(4);
});
Run Code Online (Sandbox Code Playgroud)
返回将返回主调用函数test1(); 并从那里继续test3();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
function test1(){
test2();
test3();
}
function test2(){
alert(2);
return;
test4();
test5();
}
function test3(){
alert(3);
}
function test4(){
alert(4);
}
function test5(){
alert(5);
}
test1();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但如果你只是添加''; 这将完全停止执行而不会导致任何错误.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
function test1(){
test2();
test3();
}
function test2(){
alert(2);
throw '';
test4();
test5();
}
function test3(){
alert(3);
}
function test4(){
alert(4);
}
function test5(){
alert(5);
}
test1();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是用firefox和chrome测试的.我不知道IE或Safari如何处理这个问题
Bri*_*and 15
只要打电话,die()不要定义它.你的脚本会崩溃.:)
当我这样做时,我通常会打电话discombobulate(),但原则是一样的.
(实际上,它的作用是抛出一个ReferenceError,使它与spudly的答案大致相同 - 但是为了调试目的,键入的时间更短.)
可以滚动自己的PHP版本:
function die(msg)
{
throw msg;
}
function test(arg1)
{
arg1 = arg1 || die("arg1 is missing");
}
test();
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您正在使用nodejs,则可以使用
process.exit(<code>);
Run Code Online (Sandbox Code Playgroud)
JS 中没有相当于 php die() 的函数 exit,如果你没有使用任何函数,那么你可以简单地使用 return;
return;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111509 次 |
| 最近记录: |