简体   繁体   English

如何在JavaScript中从另一个方法调用一个方法?

[英]How to call one method from another method in javascript?

I would like to know if the way I have assigned values to the 'Deval' and 'Reval' variables in the 'datevalidate' method is correct? 我想知道在“ datevalidate”方法中“ Deval”和“ Reval”变量分配值的方式是否正确?

If you notice my code, I am assigning values by specifying the entire chain pointing to the method like so ' x9.validator.check_element_val() '. 如果您注意到我的代码,我将通过指定整个指向方法的链来赋值,例如' x9.validator.check_element_val() '。 Can I make a less explicit call? 我可以打个不太明确的电话吗? Since I am trying to access a function outside the immediate lexical scope of these variables, is there some way I can use a closure to better approach this ? 由于我试图访问这些变量的直接词法范围之外的函数, 是否有某种方法可以使用闭包更好地实现此目的

Kindly correct my understanding of closures if it is not apt for the current scenario. 如果不适合当前情况,请更正我对闭包的理解。

var x9 = {} || x9;
x9.validator = {
mode : 1,
check_element_val : function(el){
    var returnval = 0;
    if (el.value == 0 || el.value == undefined || el.value == null || el.value == ''){
        returnval = 0;
    }else{
        returnval = 1;
    }
    return returnval;
},
datevalidate : function(mode, dep_el, ret_el){
    var returnobj = new Object();
    if (mode == 1){
        Deval = x9.validator.check_element_val(dep_el);
        Reval = x9.validator.check_element_val(ret_el);
        if (Deval == 0 || Reval == 0){
            returnobj.returnval = false;
        }else{
            returnobj.returnval = true;
        }                   
    }
    return JSON.stringify(returnobj);
}
};

Thanks in advance. 提前致谢。

Perhaps you would benefit from the Module pattern (see this simple example ). 也许您将从Module模式中受益(请参阅此简单示例 )。 You could do something like this: 您可以执行以下操作:

var x9 = {} || x9;
x9.validator = (function() {

    var mode = 1;

    var check_element_val = function(el){
        var returnval = 0;
        if (el.value == 0 || el.value == undefined || el.value == null || el.value == ''){
            returnval = 0;
        }else{
            returnval = 1;
        }
        return returnval;
    };

    var datevalidate = function(mode, dep_el, ret_el){
        var returnobj = new Object();
        if (mode == 1){
            Deval = check_element_val(dep_el);
            Reval = check_element_val(ret_el);
            if (Deval == 0 || Reval == 0){
                returnobj.returnval = false;
            }else{
                returnobj.returnval = true;
            }                   
        }
        return JSON.stringify(returnobj);
    }

    return {
        mode : mode,
        check_element_val : check_element_val,
        datevalidate : datevalidate
    };
})();

As a side note, I'm not sure what you were trying to do with the mode variable, but be aware that the datevalidate function will not be using it unless you supply it yourself as an argument, for instance: 附带说明一下,我不确定您要使用mode变量做什么,但是请注意,除非您自己提供它作为参数,否则datevalidate函数将不会使用它,例如:

var validation = x9.validator.datevalidate(x9.validator.mode,someValue1,someValue2);

If you want the function to only use the mode variable from within the validator, remove mode from the argument list. 如果要让函数仅使用验证器中的mode变量,请从参数列表中删除mode If you want to keep the ability to provide different modes, though, you can write an additional function. 但是,如果要保持提供不同模式的能力,可以编写一个附加功能。 For example: 例如:

var datevalidate_default_mode = function(dep_el,ret_el) {
    return datevalidate(mode, dep_el, ret_el);
};

I think it looks nicer than adding x9.validator.mode to the datevalidate function every time you want to use the default value. 我认为,每次您要使用默认值时,它都比将x9.validator.mode添加到datevalidate函数看起来更好。 And that way you can even remove mode from the module output. 这样,您甚至可以从模块输出中删除mode Either way, if you're adding a new public function, don't forget to expose it in the module output as well! 无论哪种方式,如果要添加新的公共功能,请不要忘记在模块输出中也将其公开!

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

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