[英]Function explanation using the ?: ( conditional operator in javascript )
I'm trying to understand this function that returns the ordinal numbers when we give it a number. 我试图理解这个函数,当我们给它一个数字时它返回序数。
Unfortunately I couldn't figure out how this is working with the conditional operator, could someone explain it to me? 不幸的是,我无法弄清楚条件运算符的工作方式,有人可以向我解释一下吗?
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
The best way to explain this sort of thing is to break it down into a function with if
statements. 解释这种情况的最好方法是使用
if
语句将其分解为一个函数。 Take a look at the newFunction
it does the same thing that the function getOrdinalNum
does: 看一下
newFunction
它与newFunction
函数getOrdinalNum
相同的功能:
function getOrdinalNum(n) { return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : ''); } function newFunction(n) { if (n > 0) { if ((n > 3 && n < 21) || n % 10 > 3) { return n + 'th'; // essentially returning ['th', 'st', 'nd', 'rd'][0]; } else { return n + ['th', 'st', 'nd', 'rd'][n % 10]; } } } for(let i = 1; i < 9; i++) { console.log(getOrdinalNum(i)); console.log(newFunction(i)); }
Break it down like this: 像这样分解它:
n +
(
n > 0
? ['th', 'st', 'nd', 'rd']
[
(n > 3 && n < 21) || n % 10 > 3
? 0
: n % 10
]
: ''
);
Here: 这里:
n > 0
. n > 0
。 If yes then: ['th', 'st', 'nd', 'rd']
['th', 'st', 'nd', 'rd']
[]
tells us a property accessor will follow []
告诉我们将跟随一个属性访问器 0
(which will mean ( th
) or the result of n & 10
0
(将表示( th
)或n & 10
的结果 n
was. n
。 n
is smaller or equal with 0
then whatever n
was, an empty string is added to it. n
小于或等于0
则无论n
是多少,都会向其中添加一个空字符串。 It helps to know the operator precedence in JS . 有助于了解JS中的运算符优先级 。 Give it a goooood read and practice some.
给它一个很好的阅读和练习一些。
The ternary conditional operator is different than most other operators in that it takes 3 operands instead of one or two. 三元条件运算符与大多数其他运算符的不同之处在于,它需要3个操作数而不是一个或两个。
You are used to unary operators like the negative symbol in -5
which takes one operand and makes it a negative value. 您已习惯一元运算符,如
-5
的负号,它采用一个操作数并将其设为负值。
There is also the binary concatenation operator +
used like 'hello ' + 'world'
. 还有一个二进制连接运算符
+
类似于'hello ' + 'world'
。 Here there are two operands which produce the value 'hello world'
. 这里有两个产生值
'hello world'
操作数。
The ternary conditional operator has the form 三元条件运算符具有形式
/* conditional expression */ ? /* expression if truthy */ : /* expression if not truthy*/
Where the comments are the operands for you to fill in with the more complex code from your example. 在注释是操作数的地方 ,您可以使用示例中更复杂的代码进行填充。
// if n > 0 then the complex expression, otherwise the empty string
Try to run the following statements in your browser. 尝试在浏览器中运行以下语句。
console.log(true ? 'true value' : 'false value');
var x = 3 > 1 ? 'true value' : 'false value';
console.log(x);
prompt('try entering a blank space, or characters') ? 'a' : 'b';
The code flows much the same as the other answers describe. 代码的流向与其他答案所描述的相同。 The first expression is emitted if the condition is truthy otherwise the second expression is emitted.
如果条件为真,则发出第一个表达式,否则发出第二个表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.