I was looking at these if statements:
if (cellDate < filterLocalDateAtMidnight) {
return -1;
} else if (cellDate > filterLocalDateAtMidnight) {
return 1;
} else {
return 0;
}
from this article and I was wondering why short hand if statements were not used (Ie using the ternary operator)> is there a disadvantage to using it? It seems like a good opportunity to use it.
If it can be used, is the below how you would simplify it? I'd like to refactor my code although I'm a little worried about straying from the example in the article (and inadvertently introducing some special case glitches)
cellDate < filterLocalDateAtMidnight ? -1 :(cellDate > filterLocalDateAtMidnight : 1 : 0 );
i assume the separate if statements are used in case of null/undefined values?
Nested ternaries are confusing to read and should generally avoided. See the Airbnb style guide .
With numerical comparisons you can usually just use subtraction. ie
return cellDate - filterLocalDateAtMidnight;
But beware that this solution is vulnerable to integer overflow for really large numbers.
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.