Your code snippet demonstrates the concept of a function factory, which creates a function that can check a specific property rather than returning a boolean directly.
While this approach can be handy, there are a couple of issues to consider:
Within the returned function, you are only checking a fixed value passed by the factory. Since this value remains constant (due to closure), the resulting function essentially boils down to a simplistic true/false determination, rendering it somewhat redundant.
Calling `inputChecker(x)` as if it functions like a boolean instead of a function may lead to confusion.
If your goal is straightforward property checking, a cleaner alternative could be:
var checkInput = function(field) {
if(field === '' || field === 'undefined' || field === null){
return false;
}
return true;
}
However, if you desire to generate varying checking functions based on a different variable, you could employ the function factory pattern as illustrated below:
var x = true;
var checkInput = (function (x) {
if (x === true) {
return function(field) {
if(field === '' || field === 'undefined' || field === null){
return false;
}
return true;
}
} else {
return function(field) {
//handle field differently here
}
}
}(x));
With this setup, the assignment of a specific function to `checkInput` depends on the value of `x`.