具有多个变量的三元运算符
Ternary operators with more than one variable
我已经了解了三元运算符的概念,理解符号非常简单:
desired_variable = true ? false ? "value1" : "value2";
然而,我无法理解添加第二个变量背后的合理性,即使我理解答案。举个经典例子:
var eatsPlants = false;
var eatsAnimals = false;
var category;
category = eatsPlants ? eatsAnimals ? "omnivore" : "herbivore" : eatsAnimals ? "carnivore" : undefined;
console.log(category)
在这里,一个观察:如果我将变量的顺序更改为下面的语句,函数将不起作用:
category = eatsAnimals ? eatsPlants? "carnivore" : undefined : eatsPlants ? "herbivore" : "omnivore";
console.log(category)
我的问题:为什么函数在倒置项时不起作用?当我有两个或更多变量(因此有四个或更多结果)时,我该如何选择项的顺序?
看这个例子就明白了
x ? ( y ? a : b ) : c
|
|________true ---> y ? a : b
|
|________false ---> c
- 首先检查
x
的值,如果为真,它将 运行 y ? a : b
(我添加 ()
只是为了便于阅读)
- 如果为假,它将转到
c
你可以简单理解为if/else,如果我将上面的代码改为if/else
if(x){
if(y) {
return a
} else {
return b
} else {
return c
}
}
三元表达式里面有3个表达式。但是,因为三元表达式本身就是表达式,所以您可以将三元表达式放在其他三元表达式中。上面示例中的三元表达式看起来很混乱,因为它们是多个嵌套的三元表达式。通过格式化和使用括号可以更好地消除这种混淆:
var eatsPlants = false;
var eatsAnimals = false;
var category = null;
category =
(eatsPlants ?
(eatsAnimals ? "omnivore" : "herbivore")
:
(eatsAnimals ? "carnivore" : undefined)
);
console.log(category);
要获得正确的结果,您可以将三元组分组并保持相同的决策级别。
var eatsPlants = false,
eatsAnimals = false,
category = eatsPlants
? eatsAnimals
? "omnivore"
: "herbivore"
: eatsAnimals
? "carnivore"
: undefined;
console.log(category);
您不能更改 then
和 else
部分之间的顺序,因为这会影响结果(如果您不同时否定条件)。但是,您可以更改嵌套,并编写
category = eatsPlants
? eatsAnimals
? "omnivore"
: "herbivore"
: eatsAnimals
? "carnivore"
: undefined;
或
category = eatsAnimals
? eatsPlants
? "omnivore"
: "carnivore"
: eatsPlants
? "herbivore"
: undefined;
三元运算总是需要三个操作数,例如:
inputExpression ? outputExpressionIfTrue : outputExpressionIfFalse
问题中的代码使用一些三元运算的输出作为其他运算的输入表达式,如果我们像这段代码片段一样格式化它,可以更清楚地看到这一点。在每种情况下,外部操作 returns 任何内部操作运行的结果(注释解释了哪个内部操作运行以及它 returns。)
var eatsPlants = false;
var eatsAnimals = false;
var category;
category = eatsPlants
? (eatsAnimals ? "omnivore" : "herbivore") // doesn't run because eatsPlants is false
: (eatsAnimals ? "carnivore" : undefined); //returns undefined because eatsAnimals is false
console.log(category);
category = eatsAnimals
? (eatsPlants ? "carnivore" : undefined) // doesn't run because eatsAnimals is false
: (eatsPlants ? "herbivore" : "omnivore"); // returns "omnivore" because eatsPlants is false
console.log(category);
请注意,要处理有关具有相似属性的事物类别的信息,使用对象可能会有所帮助,例如:
const carnivore = {
eatsPlants: false,
eatsAnimals: true
};
const herbivore = {
eatsPlants: true,
eatsAnimals: false
};
const omnivore = {
eatsPlants: true,
eatsAnimals: true
};
console.log("carnivore:");
console.log(" eatsPlants: " + carnivore.eatsPlants);
console.log(" eatsAnimals: " + carnivore.eatsAnimals);
console.log("herbivore:");
console.log(" eatsPlants: " + herbivore.eatsPlants);
console.log(" eatsAnimals: " + herbivore.eatsAnimals);
console.log("omnivore:");
console.log(" eatsPlants: " + omnivore.eatsPlants);
console.log(" eatsAnimals: " + omnivore.eatsAnimals);
我已经了解了三元运算符的概念,理解符号非常简单:
desired_variable = true ? false ? "value1" : "value2";
然而,我无法理解添加第二个变量背后的合理性,即使我理解答案。举个经典例子:
var eatsPlants = false;
var eatsAnimals = false;
var category;
category = eatsPlants ? eatsAnimals ? "omnivore" : "herbivore" : eatsAnimals ? "carnivore" : undefined;
console.log(category)
在这里,一个观察:如果我将变量的顺序更改为下面的语句,函数将不起作用:
category = eatsAnimals ? eatsPlants? "carnivore" : undefined : eatsPlants ? "herbivore" : "omnivore";
console.log(category)
我的问题:为什么函数在倒置项时不起作用?当我有两个或更多变量(因此有四个或更多结果)时,我该如何选择项的顺序?
看这个例子就明白了
x ? ( y ? a : b ) : c
|
|________true ---> y ? a : b
|
|________false ---> c
- 首先检查
x
的值,如果为真,它将 运行y ? a : b
(我添加()
只是为了便于阅读) - 如果为假,它将转到
c
你可以简单理解为if/else,如果我将上面的代码改为if/else
if(x){
if(y) {
return a
} else {
return b
} else {
return c
}
}
三元表达式里面有3个表达式。但是,因为三元表达式本身就是表达式,所以您可以将三元表达式放在其他三元表达式中。上面示例中的三元表达式看起来很混乱,因为它们是多个嵌套的三元表达式。通过格式化和使用括号可以更好地消除这种混淆:
var eatsPlants = false;
var eatsAnimals = false;
var category = null;
category =
(eatsPlants ?
(eatsAnimals ? "omnivore" : "herbivore")
:
(eatsAnimals ? "carnivore" : undefined)
);
console.log(category);
要获得正确的结果,您可以将三元组分组并保持相同的决策级别。
var eatsPlants = false,
eatsAnimals = false,
category = eatsPlants
? eatsAnimals
? "omnivore"
: "herbivore"
: eatsAnimals
? "carnivore"
: undefined;
console.log(category);
您不能更改 then
和 else
部分之间的顺序,因为这会影响结果(如果您不同时否定条件)。但是,您可以更改嵌套,并编写
category = eatsPlants
? eatsAnimals
? "omnivore"
: "herbivore"
: eatsAnimals
? "carnivore"
: undefined;
或
category = eatsAnimals
? eatsPlants
? "omnivore"
: "carnivore"
: eatsPlants
? "herbivore"
: undefined;
三元运算总是需要三个操作数,例如:
inputExpression ? outputExpressionIfTrue : outputExpressionIfFalse
问题中的代码使用一些三元运算的输出作为其他运算的输入表达式,如果我们像这段代码片段一样格式化它,可以更清楚地看到这一点。在每种情况下,外部操作 returns 任何内部操作运行的结果(注释解释了哪个内部操作运行以及它 returns。)
var eatsPlants = false;
var eatsAnimals = false;
var category;
category = eatsPlants
? (eatsAnimals ? "omnivore" : "herbivore") // doesn't run because eatsPlants is false
: (eatsAnimals ? "carnivore" : undefined); //returns undefined because eatsAnimals is false
console.log(category);
category = eatsAnimals
? (eatsPlants ? "carnivore" : undefined) // doesn't run because eatsAnimals is false
: (eatsPlants ? "herbivore" : "omnivore"); // returns "omnivore" because eatsPlants is false
console.log(category);
请注意,要处理有关具有相似属性的事物类别的信息,使用对象可能会有所帮助,例如:
const carnivore = {
eatsPlants: false,
eatsAnimals: true
};
const herbivore = {
eatsPlants: true,
eatsAnimals: false
};
const omnivore = {
eatsPlants: true,
eatsAnimals: true
};
console.log("carnivore:");
console.log(" eatsPlants: " + carnivore.eatsPlants);
console.log(" eatsAnimals: " + carnivore.eatsAnimals);
console.log("herbivore:");
console.log(" eatsPlants: " + herbivore.eatsPlants);
console.log(" eatsAnimals: " + herbivore.eatsAnimals);
console.log("omnivore:");
console.log(" eatsPlants: " + omnivore.eatsPlants);
console.log(" eatsAnimals: " + omnivore.eatsAnimals);