简体   繁体   English

使用?:的功能说明(javascript中的条件运算符)

[英]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: 这里:

  1. JS checks if n > 0 . JS检查n > 0 If yes then: 如果是,则:
    1. An array is created ['th', 'st', 'nd', 'rd'] 创建一个数组['th', 'st', 'nd', 'rd']
    2. The next [] tells us a property accessor will follow 下一个[]告诉我们将跟随一个属性访问器
    3. Which property is determined by the ternary operation. 哪个属性由三元运算确定。 Either 0 (which will mean ( th ) or the result of n & 10 0 (将表示( th )或n & 10的结果
    4. And the result of accessing that property is added whatever n was. 并且访问该属性的结果将加上n
  2. If 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. 给它一个很好的阅读和练习一些。

Operators (unary, binary, ternary) 运算符(一元,二进制,三元)

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

Simple example. 简单的例子。

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. 如果条件为真,则发出第一​​个表达式,否则发出第二个表达式。

Here are some docs on what I mean by truthy 这是一些关于我所说的真实的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM