Ternary Operator in JavaScript With Multiple Expressions?

后端 未结 6 1987
再見小時候
再見小時候 2021-01-17 14:29
the_styles ? the_styles.appendTo(\'head\'); the_styles=null : the_styles = $(\'.stylesheet\').detach();

Obviously, this isn\'t valid. Notice the \"

6条回答
  •  囚心锁ツ
    2021-01-17 14:59

    Use the comma operator this way:

    the_styles ? (the_styles.appendTo('head'), the_styles=null) : the_styles =  $('.stylesheet').detach();
    

    Here's what the Mozilla Developer Center writes about the comma operator:

    You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

    Read more here: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Comma_Operator

提交回复
热议问题