Javascript shorthand
If I can say:
var big = (x > 10) ? true : false;
instead of:
var big;
if (x > 10) {
big = 开发者_Go百科true;
}
else {
big = false;
}
how do I make this similarly shorter?
var now = new Date
if (now.getHours() < 5) {
return "late night pick me up";
}
else if (now.getHours() < 9) {
return "breakfast";
}
else if (now.getHours() < 13) {
return "lunch";
}
else if (now.getHours() < 17) {
return "afternoon snak";
}
else {
return "dinner";
}
Thanks a big bunch!
var now = new Date().getHours();
return now < 5 ? "late night pick me up" :
now < 9 ? "breakfast" :
now < 13 ? "lunch" :
now < 17 ? "afternoon snak" : "dinner";
You can't, without a bunch of messy nested ternary operators. The ternary operator is only good for one liners.
You can't shorthand that particularly, personally I would just write it like this:
if (now.getHours() < 5) return "late night pick me up";
else if (now.getHours() < 9) return "breakfast";
else if (now.getHours() < 13) return "lunch";
else if (now.getHours() < 17) return "afternoon snak";
else return "dinner";
That's not too bad is it?
You probably don't want to, as arguably it would be less readable. However, you could simply nest the ternary operator like so:
var now = (now.getHours() < 5) ? "late night pick me up" : ((now.getHours() < 9) ? "breakfast" : ((now.getHours() < 13) ? "lunch" : ((now.getHours() < 17) ? "afternoon snack" : "dinner")))));
I hope you can see why this isn't a good idea!
A longer, more complex condition like this generally needs multiple lines and good block separation to be easily understandable - and while you could arguably add line breaks, the standard if-else
blocks are ultimately going to come out the winner at clearly expressing your intent.
First, try var big = x > 10;
instead of var big = (x > 10) ? true : false;
Second, you don't need if-else when you have return.
//looks a little confusing, but you can move the return to line after ifs
var now = new Date
if (now.getHours() < 5) return "late night pick me up";
if (now.getHours() < 9) return "breakfast";
if (now.getHours() < 13) return "lunch";
if (now.getHours() < 17) return "afternoon snak";
return "dinner";
It will look quite messy:
return ((now.getHours() < 5)?"late night pick me up":
((now.getHours() < 9)?"breakfast":
((now.getHours() < 13)?"lunch":
((now.getHours() < 17)?"afternoon snack":
"dinner"
)
)
)
);
You have to remember to match parenthesis.
You could always use a switch statement:
switch(true)
{
case (now.getHours() < 5):
return "late night pick me up";
break;
case (now.getHours() < 9):
return "breakfast";
break; //etc...
default:
return "dinner";
}
I stumbled across this in search for something else. Here's a little shorthand trick using logical operators and abusing the fact string literals are truthy =]
var hr = new Date().getHours();
return hr < 5 && 'late night pick me up'
|| hr < 9 && 'breakfast'
|| hr < 13 && 'lunch'
|| hr < 17 && 'afternoon snack'
|| 'dinner';
You can always use a table to replicate functionality:
var now = new Date();
var meals = [
"late night pick me up",
"breakfast",
"lunch",
"afernoon snack",
"dinner"];
return meals[parseInt(now.getHours()-4)/4];
精彩评论