提早退出功能? [英] Early exit from function?

查看:115
本文介绍了提早退出功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能:

function myfunction() {
  if (a == 'stop')  // How can I stop the function here?
}

有没有类似 exit()

推荐答案

您只需使用返回

function myfunction() {
     if(a == 'stop') 
         return;
}

这将发送返回值 undefined 到任何调用函数。

This will send a return value of undefined to whatever called the function.

var x = myfunction();

console.log( x );  // console shows undefined

当然,您可以指定不同的返回值。无论返回什么值,都将使用上面的示例记录到控制台。

Of course, you can specify a different return value. Whatever value is returned will be logged to the console using the above example.

return false;
return true;
return "some string";
return 12345;

这篇关于提早退出功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆