什么是“返回”;在javascript中意味着什么? [英] What does "return;" mean in javascript?

查看:200
本文介绍了什么是“返回”;在javascript中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转过这段代码:

if(!function1()) return;
function2();
function3(array1[index1]);

返回后即时分号是什么意思?我在jquery代码下找到了它。 jquery与它无关吗?

What does having an instant semicolon after return mean? I found it under a jquery code. jquery has nothing to do with it right?

推荐答案

返回语句在Javascript中终止一个函数。这段代码基本上说如果!function1()(与 function1()的返回值相反)是 true 或truthy,然后停止执行函数。

The return statement in Javascript terminates a function. This code basically says that if !function1() (the opposite of function1()'s return value) is true or truthy, then just stop executing the funciton.

也就是说,立即终止此函数。分号是javascript中的语句终止符。如果!function1()最终为 true 或truthy,那么该函数将返回,并且下面没有任何代码将被执行。

Which is to say, terminate this function immediately. The semicolon is a statement terminator in javascript. If !function1() ends up being true or truthy, then the function will return and none of the code below it will be executed.

返回可用于从函数返回值,如:

Return can be used to return a value from a function, like:

function returnFive(){
   return 5;
}

或者,就像在这种情况下,它是一种简单的方法来停止一个功能,如果 - 出于某种原因 - 我们不再需要继续。喜欢:

Or, like in this case, it's an easy way to stop a function if--for some reason--we no longer need to continue. Like:

function isEven(number){
  /* If the input isn't a number, just return false immediately */
  if(typeof number !== 'number') return false;
   /* then you can do even/odd checking here */
}

正如您所怀疑的,这与jQuery没有任何关系。分号在那里,以便Javascript引擎知道有两个语句:

As you suspected, this has nothing to do specifically with jQuery. The semicolon is there so that the Javascript engine knows that there are two statements:

if(!function1()) return;
        function2();

不只是一个陈述,你可以看到它会完全改变程序:

Not just one statement, which you can see would totally change the program:

if(!function1()) return function2();

这篇关于什么是“返回”;在javascript中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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