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

问题描述:

所以我看到很多JavaScript代码(我自己写过一些代码)就像

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

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

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

以及其他时间:

callSomeFunction();
return true;

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

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函数后使用return true或返回false?

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

我想很多次你看到这个代码,来自那些习惯于表单,按钮,输入和类似事件的事件处理程序的人。

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.

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



Basically, when you have something like:

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

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

callSomeFunction()返回true,然后是表单 a 将提交,否则不会。

and callSomeFunction() returns true, then the form or a will submit, otherwise it won't.

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

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.