When i write a function in JS or jQuery and a parameter is required, i use the if something.length trick…
this.example = function(e) {
if ($(e).length) {
/*blablabla*/
} else {
return false;
}
}
But i dont want to do this in all functions. Is there a way to generalize this?
like:
this.ck = function(e) {
return function(e){
if (!(e.length)) { return false;}
}
}
this.example = function(e) {
ck(e)
/*blablabla*/
}
Maybe this, but see below:
You’d use that like this:
I would however suggest that instead of writing functions that take jQuery objects as parameters, you write those functions as your very own jQuery plugins.