至少,您可以将验证规则分解为自己的功能:
function IsValid(x) { return (x != undefined && x != ""); } var x = ""; var y = ""; var z = ""; //... //...variables get set if(IsValid(x)) doSomething(); if(IsValid(y)) doSomething(); if(IsValid(z)) doSomething();
除了joel所说的你还可以做以下事情:
function doOnValid(x, func){ if(x != undefined && x != "") func(); }
然后你可以这样做:
doOnValid(x, doSomething); doOnValid(y, doSomethingElse); doOnvalid(z, function() { /*Yay lambda function*/ });