为什么我的原型方法返回未定义

Why is my prototype method returning undefined?

本文关键字:返回 未定义 方法 原型 我的 为什么      更新时间:2023-09-26

我创建了一个返回true和false的原型方法。问题是它没有返回值。我知道它到达了语句,因为当条件满足但不返回值时,我使用了console.log()。

我似乎想不通。如果有人能在这里帮我,我真的很感激。下面是我的代码

function Validation(){
    this.checkRequired = function(){
        $(".modifiers-group-cont").each(function(){
            var select_opt_notify = $(this).find(".select-option-notify");
            /*The radio buttons that are required  are always going to be marked check,thats why we are only
            * checking for the checkbox*/
            if($(this).attr("data-is-checked") == "false" && $(this).attr("data-input-type") == "checkbox"){
              select_opt_notify.show();
                return  false;
            } else {
                select_opt_notify.hide();
                return "true";
            }
        });
    }
function ModifierPost(){
    (function(){
        $("#add-to-cart").click(function(){
            console.log(validation.constructor); //Shows undefined
            if(validation.checkRequired()){
            $.post("#",$("form").serialize()+ "&item_id=" + item_id);
            }
        });
    })();
}

return语句属于each:中的函数

$(".modifiers-group-cont").each(function(){

您需要将要返回的值公开到外部作用域,并在调用each之后调用return

您已经使用大写V创建了函数,并在验证函数名称中使用Smaill V调用这就是它给你在代码中提到的未定义位置的原因。

 console.log(validation.constructor); //Shows undefined
 if(validation.checkRequired()){

应该像一样使用大写V

console.log(Validation.constructor); //Shows undefined
if(Validation.checkRequired()){