javascript return true 或 return false 何时以及如何使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19166296/
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-10-27 14:38:00  来源:igfitidea点击:

javascript return true or return false when and how to use it?

javascript

提问by oliverdejohnson

So I see a lot of JavaScript code (have written some myself) that does something like

所以我看到很多 JavaScript 代码(我自己写了一些)做类似的事情

<script>
function CallSomeFunction()
{
   //function body
}

$(document).ready(function () {
 callSomeFunction();
 return false;
});

and at other times:

其他时间:

callSomeFunction();
return true;

Basically I've never understood the true use of the return true/false after function calls in JavaScript. I've just assumed its some kind of magic I need my functions to run well.

基本上我从来没有理解在 JavaScript 中函数调用后返回 true/false 的真正用途。我只是假设它有某种魔力,我需要我的功能运行良好。

So please I'd like to know why? why do we use return true or return false after calling a JavaScript function?

所以请我想知道为什么?为什么我们在调用 JavaScript 函数后使用 return true 或 return false?

采纳答案by Eric Hotinger

I think a lot of times when you see this code, it's from people who are in the habit of event handlers for forms, buttons, inputs, and things of that sort.

我认为很多时候当你看到这段代码时,它来自那些习惯于为表单、按钮、输入和这类东西使用事件处理程序的人。

Basically, when you have something like:

基本上,当你有类似的东西时:

<form onsubmit="return callSomeFunction();"></form>

or

或者

<a href="#" onclick="return callSomeFunction();"></a>`

and callSomeFunction()returns true, then the formor awill submit, otherwise it won't.

callSomeFunction()返回true,则formora将提交,否则不会。

Other more obvious general purposes for returning true or false as a result of a function are because they are expected to return a boolean.

作为函数结果返回 true 或 false 的其他更明显的一般目的是因为它们应该返回一个布尔值。

回答by Eric Hotinger

Your code makes no sense, maybe because it's out of context.

您的代码毫无意义,可能是因为它脱离了上下文。

If you mean code like this:

如果你的意思是这样的代码:

$('a').click(function () {
    callFunction();
    return false;
});

The return false will return false to the click-event. That tells the browser to stop following events, like follow a link. It has nothing to do with the previous function call. Javascript runs from top to bottom more or less, so a line cannot affect a previous line.

return false 将返回 false 给点击事件。这告诉浏览器停止关注事件,比如关注链接。它与之前的函数调用无关。Javascript 或多或少地从上到下运行,因此一行不会影响前一行。

回答by prashant

returning true or false indicates that whether execution should continue or stop right there. So just an example

返回 true 或 false 表示执行应该继续还是停止。所以只是一个例子

<input type="button" onclick="return func();" />

Now if func() is defined like this

现在如果 func() 是这样定义的

function func()
{
 // do something
return false;
}

the click event will never get executed. On the contrary if return true is written then the click event will always be executed.

click 事件永远不会被执行。相反,如果 return true 被写入,则单击事件将始终被执行。