提前退出功能?
2022-08-29 23:13:17
我有一个函数:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
在 JavaScript 中是否有类似的东西?exit()
我有一个函数:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
在 JavaScript 中是否有类似的东西?exit()
你可以只使用.return
function myfunction() {
if(a == 'stop')
return;
}
这会将返回值发送到任何称为函数的函数。undefined
var x = myfunction();
console.log( x ); // console shows undefined
当然,您可以指定不同的返回值。返回的任何值都将使用上述示例记录到控制台。
return false;
return true;
return "some string";
return 12345;
显然,你可以这样做:
function myFunction() {myFunction:{
console.log('i get executed');
break myFunction;
console.log('i do not get executed');
}}
通过使用标签查看阻止范围:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
我还看不到任何缺点。但这似乎不是一个常见的用途。
得出这个答案:JavaScript相当于PHP的骰子