javascript 返回多个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11385438/
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
Return multiple functions
提问by user1510389
I am new to javascript, so I apologize if this is all totally wrong, but I'm trying to validate a form right now, and I have the simple functions for testing. I want the function validateForm() to return all three of the functions checkName(), checkEmail, and checkMessage(). The way I have the validateForm() function, it only runs the checkName() function. Any ideas?
我是 javascript 新手,所以如果这完全是错误的,我深表歉意,但我现在正在尝试验证表单,并且我有简单的测试功能。我希望函数validateForm() 返回所有三个函数checkName()、checkEmail 和checkMessage()。我使用 validateForm() 函数的方式,它只运行 checkName() 函数。有任何想法吗?
function checkName(){
var name=document.forms["contactForm"]["Name"].value;
if(name==null || name==""){
$("input#name").css("border-color", "#ff0000");
$("input#name").attr("placeholder","Your name is required");
return false;
}
else {
$("input#name").css("border-color", "#00a8ff");
$("input#name").attr("placeholder","");
return true;
}
}
function checkEmail(){
var email=document.forms["contactForm"]["Email"].value;
if(email==null || email==""){
$("input#email").css("border-color", "#ff0000");
$("input#email").attr("placeholder","Your email is required");
return false;
}
else {
$("input#email").css("border-color", "#00a8ff");
$("input#email").attr("placeholder","");
return true;
}
}
function checkMessage(){
var message=document.forms["contactForm"]["Message"].value;
if(message==null || message==""){
$("textarea#message").css("border-color", "#ff0000");
$("textarea#message").attr("placeholder","Your message is required");
return false;
}
else {
$("textarea#message").css("border-color", "#00a8ff");
$("textarea#message").attr("placeholder","");
return true;
}
}
function validateForm(){
return checkName() && checkEmail() && checkMessage();
}
回答by Dmitry Osinovskiy
Operator && executes left operand (checkName in your case), and, if it is false, immediately returns false without executing right operand. So, you need to manually execute each of your function and only then connect them via &&.
运算符 && 执行左操作数(在您的情况下为 checkName ),如果为 false,则立即返回 false 而不执行右操作数。因此,您需要手动执行每个函数,然后才通过 && 连接它们。
function validateForm(){
var a = checkName();
var b = checkEmail();
var c = checkMessage();
return a && b && c;
}
回答by Rajib Banerjee
var yourdomain = {};
yourdomain.validateFormField = function(){
var domNameElement = document.getElementById("name");
var domMailElement = document.getElementById("mail");
var domMsgElement = document.getElementById("msg");
return {
validateName: function (){
/*your checkName() code goes here with a function return*/;
`enter code here`},
validateMail: function (){
/*your checkEmail() code goes here with a function return*/;
},
validateMsg: function (){
/*your checkMessage() code goes here with a function return*/;
},
};
}
You can call it as bellow.
你可以称之为波纹管。
yourdomain.validateFormField.validateName()
yourdomain.validateFormField.validateName()
or
或者
yourdomain.validateFormField.validateMail()
yourdomain.validateFormField.validateMail()
or
或者
yourdomain.validateFormField.validateMsg()
yourdomain.validateFormField.validateMsg()
depending upon the need.
视需要而定。
回答by Michael Reno Pratama Setyawan
In my javascript project I did this.
在我的 javascript 项目中,我这样做了。
return {
checkName : checkName,
checkEmail : checkEmail,
checkMessage : checkMessage,
validateForm : validateForm
};
回答by Gandarez
I did a bit different from others. Created an array, added all results of my validation methods and than compared each value (AND);
我做的和其他人有点不同。创建一个数组,添加我的验证方法的所有结果,然后比较每个值(AND);
function ValidateInputs() {
var arrValidations = new Array();
arrValidations.push(ValidateProponentDocument());
arrValidations.push(ValidateProponentName());
arrValidations.push(ValidateProponentBirthdate());
var ret = true;
$.each(arrValidations, function (index, item) {
ret &= item;
});
return ret;
}
回答by RobB
The way that you have it currently tries to evaluate them in order due to short-circuit evaluation. If one of the items return false then the process ends.
由于短路评估,您目前拥有的方式尝试按顺序评估它们。如果其中一项返回 false,则该过程结束。
The short-circuit expression x Sand y (using Sand to denote the short-circuit variety) is equivalent to the conditional expression if x then y else false; the expression x Sor y is equivalent to if x then true else y.
短路表达式 x Sand y(使用 Sand 表示短路变体)等价于条件表达式 if x then y else false;表达式 x Sor y 等价于 if x then true else y。
You want to call the functions separately to execute each check:
您想分别调用函数来执行每个检查:
function validateForm(){
var results = [];
result[0] = checkName();
result[1] = varcheckEmail();
result[2] = checkMessage();
}
回答by Ben Parsons
If checkName() is false, then the other functions won't be executed because the result would still be false.
如果 checkName() 为 false,则不会执行其他函数,因为结果仍然为 false。
回答by Daniel Baulig
What you encounter is called short circuiting. Javascript logical operators do short circuits. That means if you have the expression test1() && test2()
and the first function returns false, then the whole expression will always evaluate to false, nevertheless what the second function returns. Thus the second function is never called. If the first function returns true, the second function will be called however, since it actually influences the evaluation of the expression.
您遇到的情况称为短路。Javascript 逻辑运算符会短路。这意味着如果您有表达式test1() && test2()
并且第一个函数返回 false,那么整个表达式将始终计算为 false,但第二个函数返回什么。因此永远不会调用第二个函数。如果第一个函数返回 true,则将调用第二个函数,因为它实际上会影响表达式的计算。
Thus, if checkName()
returns false, then the other functions are not called. If it returns true, then checkEmail()
will be called. If it also returns true, then checkMessages()
will also be called. If any of the functions returns false however, the remaining functions will not be evaluated due to short circuiting.
因此,如果checkName()
返回 false,则不会调用其他函数。如果它返回true,checkEmail()
则将被调用。如果它也返回 true,则checkMessages()
也将被调用。但是,如果任何函数返回 false,则其余函数将不会由于短路而被评估。