I would like to know if the way I have assigned values to the 'Deval' and 'Reval' variables in the 'datevalidate' method is correct?
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() '. 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 ). 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:
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. 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. And that way you can even remove mode
from the module output. Either way, if you're adding a new public function, don't forget to expose it in the module output as well!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.