简体   繁体   English

javascript return true 或 return false 什么时候用,怎么用?

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

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.基本上我从来没有理解在 function 调用 JavaScript 之后返回真/假的真正用途。我只是假设它有某种魔力,我需要我的函数运行良好。

So please I'd like to know why?所以我想知道为什么? why do we use return true or return false after calling a JavaScript function?为什么我们在调用 JavaScript function 后使用 return true 或 return false?

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 form or a will submit, otherwise it won't. callSomeFunction()返回true,那么forma将提交,否则将无法。

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的其他更明显的一般目的是因为它们需要返回一个布尔值。

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. return false将返回false给click事件。 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. Javascript或多或少地从上到下运行,因此一行不会影响前一行。

returning true or false indicates that whether execution should continue or stop right there. 返回true或false表示执行是继续还是在那里停止。 So just an example 所以只是一个例子

<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. click事件永远不会被执行。 On the contrary if return true is written then the click event will always be executed. 相反,如果返回true,则会始终执行click事件。

This works for me:这对我有用:

<form action="javascript:void(0);" onsubmit="return checkForm(this);">

People often use a variable's Boolean value to determine whether or not it was properly defined.人们经常使用变量的布尔值来确定它是否被正确定义。
Similarly, it's occasionally used to confirm that a function was completed successfully.同样,它偶尔也用于确认某个功能是否已成功完成。

When writing a method that doesn't return anything, it's good practice to return a true value anyway.在编写不返回任何内容的方法时,无论如何都返回一个真值是一种很好的做法。
This could be substituted for any other value deemed “truthy”, like 1 or a non-empty string.这可以替换为任何其他被视为“真实”的值,例如 1 或非空字符串。

For example, if I wrote “console.log('Hello, ') && console.log('World!');”, only the first log would appear, since console.log doesn't return true (at least in my IDE).例如,如果我写了“console.log('Hello, ') && console.log('World!');”,则只会出现第一个日志,因为 console.log 不会返回 true(至少在我的IDE)。 If it did, both would appear.如果是这样,两者都会出现。
Of course, this extends to any Boolean-related operation, like if-else statements, switches, and ternary.当然,这扩展到任何与布尔相关的操作,如 if-else 语句、开关和三元。

Regarding the web example, I'm pretty sure jQuery uses the aforementioned system for cycling through DOM event listener callbacks.关于 web 示例,我很确定 jQuery 使用上述系统循环遍历 DOM 事件侦听器回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM