I have created a validate()
function in JavaScript, and when I invoke the method it returns undefined
.... everything in my code looks right to me, why is it doing that?
我在JavaScript中创建了一个validate()函数,当我调用该方法时,它返回undefined ....我的代码中的所有内容对我来说都是正确的,为什么这样做呢?
function validate() {
var __self = this;
__self.is_valid = true;
$(document).on("click", "#login,#create_account", function() {
var email = $("#email").val(),
password = $("#password").val();
if (email == "") {
$("#email-err-msg").removeClass("hidden").html("Enter an email address");
__self.is_valid = false;
} else {
$("#email-err-msg").addClass("hidden")
}
return __self.is_valid;
});
}
(function logUser() {
$(document).on("submit", "form", function() {
var action = $(".action").attr("data-account-action");
console.log(validate());
if (!validate()) {
return false;
}
});
})();
1 个解决方案
#1
0
Your validate
function never returns anything, so the result of calling it is always undefined
. You have a callback to an event handler that you define within it, and you have a return
statement in that, but none in validate
.
您的验证函数永远不会返回任何内容,因此调用它的结果始终是未定义的。您有一个回调到您在其中定义的事件处理程序,并且您有一个return语句,但在validate中没有。
It's not clear why you are hooking up an event handler every time the user tries to submit a form. I would suggest not doing that. Perhaps something like:
每次用户尝试提交表单时,为什么要连接事件处理程序并不清楚。我建议不这样做。也许是这样的:
function validate() {
var is_valid = true;
var email = $("#email").val(),
password = $("#password").val();
if (email == "") {
$("#email-err-msg").removeClass("hidden").html("Enter an email address");
is_valid = false;
} else {
$("#email-err-msg").addClass("hidden")
}
return is_valid;
}
(function logUser() {
$(document).on("submit", "form", function() {
var action = $(".action").attr("data-account-action");
return validate();
});
})();
I've removed the click
event handler as I couldn't see that there was any reason for it, removed the global variable __is_valid
,1 and returns the result of calling validate
directly from the submit
handler (since returning true
is harmless).
我已经删除了click事件处理程序,因为我看不出它有任何原因,删除了全局变量__is_valid,1并直接从提交处理程序返回调用validate的结果(因为返回true是无害的)。
1 Why was it a global variable? Because you were calling validate like this: validate()
, which means that within the call, this
refers to the global object (unless you're using strict mode, in which case it would be undefined
; but I know you aren't because you said it returned undefined
rather than throwing an error). Adding properties to the global object creates global variables.
1为什么它是一个全局变量?因为你这样调用validate:validate(),这意味着在调用中,这引用了全局对象(除非你使用严格模式,在这种情况下它将是未定义的;但我知道你不是因为你说它返回undefined而不是抛出错误)。向全局对象添加属性会创建全局变量。
#1
0
Your validate
function never returns anything, so the result of calling it is always undefined
. You have a callback to an event handler that you define within it, and you have a return
statement in that, but none in validate
.
您的验证函数永远不会返回任何内容,因此调用它的结果始终是未定义的。您有一个回调到您在其中定义的事件处理程序,并且您有一个return语句,但在validate中没有。
It's not clear why you are hooking up an event handler every time the user tries to submit a form. I would suggest not doing that. Perhaps something like:
每次用户尝试提交表单时,为什么要连接事件处理程序并不清楚。我建议不这样做。也许是这样的:
function validate() {
var is_valid = true;
var email = $("#email").val(),
password = $("#password").val();
if (email == "") {
$("#email-err-msg").removeClass("hidden").html("Enter an email address");
is_valid = false;
} else {
$("#email-err-msg").addClass("hidden")
}
return is_valid;
}
(function logUser() {
$(document).on("submit", "form", function() {
var action = $(".action").attr("data-account-action");
return validate();
});
})();
I've removed the click
event handler as I couldn't see that there was any reason for it, removed the global variable __is_valid
,1 and returns the result of calling validate
directly from the submit
handler (since returning true
is harmless).
我已经删除了click事件处理程序,因为我看不出它有任何原因,删除了全局变量__is_valid,1并直接从提交处理程序返回调用validate的结果(因为返回true是无害的)。
1 Why was it a global variable? Because you were calling validate like this: validate()
, which means that within the call, this
refers to the global object (unless you're using strict mode, in which case it would be undefined
; but I know you aren't because you said it returned undefined
rather than throwing an error). Adding properties to the global object creates global variables.
1为什么它是一个全局变量?因为你这样调用validate:validate(),这意味着在调用中,这引用了全局对象(除非你使用严格模式,在这种情况下它将是未定义的;但我知道你不是因为你说它返回undefined而不是抛出错误)。向全局对象添加属性会创建全局变量。