本文介绍了具有多个操作的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有多个操作可以执行每个案例时,我可以使用三元运算符吗?

Can I use a ternary operator when I have more than one operation to perform per case?

例如我可以在这里使用它吗?:

For example can I use it here?:

    if (dwelling) {
        dwelling = dwelling[0].nodeValue;      //first operation
        letterDwelling = dwelling[0].toUpperCase(); //second operation
 } else {
        dwelling = "";
        letterDwelling = "";
}

我只使用了这个允许后续操作的语法:

I've only used this syntax which allows one subsequent operation:

dwelling = dwelling ? dwelling[0].nodeValue : "";


推荐答案

虽然为了便于阅读,我强烈反对它和扩展你可以:

Although i highly advice against it for the sake of readability and extensibility you could:

dwelling ? (dwelling = dwelling[0].nodeValue, letterDwelling=dwelling[0].toUpperCase()) : (dwelling = letterDwelling = "");

这篇关于具有多个操作的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 05:52