[英]Javascript shorthand
If I can say: 如果我可以说:
var big = (x > 10) ? true : false;
instead of: 代替:
var big;
if (x > 10) {
big = 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. 这样一个更长,更复杂的条件通常需要多行和良好的块分离才能容易理解 - 虽然你可以说可以添加换行符,但标准的
if-else
块最终会成为明确表达你意图的赢家。
First, try var big = x > 10;
首先,尝试
var big = x > 10;
instead of var big = (x > 10) ? true : false;
而不是
var big = (x > 10) ? true : false;
var big = (x > 10) ? true : false;
Second, you don't need if-else when you have return. 其次,当你返回时,你不需要if-else。
//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";
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 =]
这是一个使用逻辑运算符和滥用事实的简写技巧字符串文字是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';
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语句:
switch(true)
{
case (now.getHours() < 5):
return "late night pick me up";
break;
case (now.getHours() < 9):
return "breakfast";
break; //etc...
default:
return "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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.