Ternary operator with multiple operations

拥有回忆 提交于 2019-12-02 05:47:32

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 = "");

To avoid the side effects using the comma-notation you could use self-invoking functions instead which can handle your code:

(foo == bar) ? doSomething() : (function(){
    // here you can write all your code
    // and even return something useful
})();

Yes, see: Conditional (ternary) Operator

var dwelling = true;
(dwelling) ? (
    dwelling = 'a',      //first operation
    letterDwelling = 'a' //second operation
) : (
    dwelling = 'b',
    letterDwelling = 'b'
);
alert(dwelling);

jsfiddle example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!