Sim*_*mon 380 javascript
我有一个功能:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
Run Code Online (Sandbox Code Playgroud)
是否有类似exit()JavaScript的东西?
use*_*716 603
你可以使用return.
function myfunction() {
if(a == 'stop')
return;
}
Run Code Online (Sandbox Code Playgroud)
这将发送返回值undefined到任何调用函数.
var x = myfunction();
console.log( x ); // console shows undefined
Run Code Online (Sandbox Code Playgroud)
当然,您可以指定不同的返回值.无论返回什么值,都将使用上面的示例记录到控制台.
return false;
return true;
return "some string";
return 12345;
Run Code Online (Sandbox Code Playgroud)
CMC*_*kai 48
显然你可以这样做:
function myFunction() {myFunction:{
console.log('i get executed');
break myFunction;
console.log('i do not get executed');
}}
Run Code Online (Sandbox Code Playgroud)
通过使用标签查看块范围:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
我还看不到任何缺点.但它似乎并不常见.
得出这个答案:PHP相当于PHP的死
Rob*_*Rob 17
这个:
function myfunction()
{
if (a == 'stop') // How can I stop working of function here?
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
xla*_*aok 17
function myfunction() {
if(a == 'stop')
return false;
}
Run Code Online (Sandbox Code Playgroud)
return false; 比只是好多了 return;
Rod*_*edo 10
使用一种不同的方法,您可以使用try catchthrow语句.
function name() {
try {
...
//get out of here
if (a == 'stop')
throw "exit";
...
} catch (e) {
// TODO: handle exception
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
726247 次 |
| 最近记录: |