JavaScript 函数末尾的返回语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2385920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:03:21  来源:igfitidea点击:

Return statements at the end of a JavaScript function

javascript

提问by Praveen Prasad

I have seen in many times in JavaScript code people add a return trueat the end although not necessary. Does anyone know why?

我在 JavaScript 代码中多次看到人们return true在末尾添加了一个,尽管不是必需的。有谁知道为什么?

var _globalString;
function doSomething()
{
    _globalString= _globalString +' do something';
    //some codes to do something more

    //finally adding a return true
    return true;
}

回答by Erik

The thing that may have gotten some people into the habit was event handlers for forms, if you have, say:

可能让一些人养成习惯的是表单的事件处理程序,如果你有的话,说:

<form onsubmit="return myfunction();">

and myfunction()returns true, the form submits, else if it returns false it doesn't. People doing it in general could've got the habit from this. Some languages require return values from functions, Javascript doesn't; and having return trueat the end of most functions serves no purpose.

myfunction()返回true,表单提交,否则如果返回false,则不会。这样做的人一般都可以从中养成习惯。有些语言需要函数的返回值,Javascript 不需要;并且return true在大多数功能的末尾没有任何用处。

回答by Bren G.

A "return" inside a function automatically stops further execution of that function so for example:

函数内的“返回”会自动停止该函数的进一步执行,例如:

function myFunc(){
    if(foo == 'bar'){
     /* do something */
    }else{
     /* do something */
    }
}

is the same as:

是相同的:

function myFunc(){
    if(foo == 'bar'){
        /* do something */
        return true;
    }

    /* if foo != 'bar' then whatever follows is executed... */

}

回答by Nazmul

In addition to Erik's answer I would like to add

除了埃里克的回答,我想补充

return true/ return falseare also used when you want boolean value as a return. And based on that return you execute some other function.

当您希望布尔值作为返回值时,也可以使用return true/ return false。并根据该返回值执行其他一些函数。

回答by Pavunkumar

Actually if you are calling the function in onsumbit event

实际上,如果您在 onsumbit 事件中调用该函数

Example

例子

 

<input type=sumit value=click Onsumbit='return function_name();">

While you are calling like , if the function return true only, form will be submit

当您调用 like 时,如果该函数仅返回 true,则表单将被提交

If it return false , it wont submit the form

如果返回 false ,则不会提交表单

回答by Jawad Anwar

Also you do not need to use return true or false in this case below

在下面的这种情况下,您也不需要使用 return true 或 false

var newPage = "http://www.google.com";
function redirectURL(){
   window.location.href= newPage;
   return true;      //no return required
}

回答by Tobias Cohen

It's hard to say why some programmers do certain things.

很难说为什么有些程序员会做某些事情。

Maybe it's intended to indicate success/failure, but they haven't added any failing branches yet?

也许它旨在表明成功/失败,但他们还没有添加任何失败的分支?