this is the statement
if( validator(foo) && foo.var1 || foo.var2 || foo.var3 || foo.var4){
//do sometihng
}else{
//do anything because if foo.* doesnt exists i cant do anything
}
the statement says: validator have to valid foo, and return true, and var1 or var2 var3 or var4 have to exists.
if someone needs an explanation, just add a commentary. This is a simple question, but im trying to get performance in my code.
by the way. There is any book or tutorial that has some info about performance code in javascript.??
thank you all!
If validator(foo) is an expensive operation, you can reverse the order of the tests:
if ((foo.var1 || foo.var2 || foo.var3 || foo.var4) && validator(foo)) {
} else {
}
If validator(foo) isn't particularly expensive, there probably is no reason to spend any time at all thinking about how to improve the performance of this little piece of code. There are better uses for your time.
By the way, this code is based on your verbal description. Your code as it stands is incompatible with the verbal description and is likely buggy. In JavaScript, false && false || true evaluates to true. You probably need
if( validator(foo) && (foo.var1 || foo.var2 || foo.var3 || foo.var4)) {
Also, as Benjamin points out, foo.var1 does not test that foo.var1 exists; it checks that it exists (either in foo or in its prototype chain) and furthermore is not a "falsy" value. (For instance, if foo.var1 is 0, then it will evaluate as false.) You might be looking for foo.hasOwnProperty('var1'), etc.