javascript - 有人可以帮我理解这个代码逻辑,它是如何工作的吗?
问题描述
var eatsPlants = true;
var eatsAnimals = false;
var category = eatsPlants ? (eatsAnimals ? "omnivore" : "herbivore") : (eatsAnimals ? "carnivore" : "undefined");
console.log(category);
//我不明白它是如何检查多个事物的代码
解决方案
var category = eatsPlants ? (eatsAnimals ? "omnivore" : "herbivore") : (eatsAnimals ? "carnivore" : "undefined");
嗯,这和
if(eatsPlants)
if(eatsAnimals) category = "omnivore";
else category = "herbivore";
else
if(eatsAnimals) category = "carnivore";
else category = "undefined";
该条件运算符的结构如下:
(condition) ? (outcome if condition is true) : (outcome if condition is false)
推荐阅读
- ruby - 替换 URL 字符串中的主机名
- android - 包含 proteus 库时出错 - “android.support.v7.appcompat.R$layout”
- wpf - 在 WPF 中使用 DevExpress ChartControl 和数据网格
- nginx - Nginx反向代理后面的Qtorrent Web GUI未加载登录网页css
- ios - 填充集合视图内的图像视图
- python-3.x - PYQT Selected Combobox 项目应删除另一个组合框中的项目
- visual-studio - 没有匹配的配置文件 [对于转移的应用程序]
- airflow - 如何将 BigQueryOperator 与 execution_date 一起使用?
- java - Tomcat 停止问题 + Spring Boot + 服务
- python - 使用 facebook 先知在具有多个时间序列的数据框中进行时间序列预测