Osc*_*son 11 javascript jquery conditional ternary-operator
the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach();
Run Code Online (Sandbox Code Playgroud)
显然,这是无效的.注意";" appendTo()
和之间the_styles=null
.我如何在1行上写它仍然有这样的多个表达式?
And*_*lio 21
以这种方式使用逗号运算符:
the_styles ? (the_styles.appendTo('head'), the_styles=null) : the_styles = $('.stylesheet').detach();
Run Code Online (Sandbox Code Playgroud)
以下是Mozilla开发人员中心撰写的有关逗号运算符的内容:
如果要在需要单个表达式的位置包含多个表达式,可以使用逗号运算符.此运算符的最常见用法是在for循环中提供多个参数.
在此处阅读更多内容:https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Comma_Operator
谁需要三元运算符?
?the_styles = !the_styles && $('.stylesheet').detach()???? ||
the_styles.appendTo('head') && null;?
Run Code Online (Sandbox Code Playgroud)
不得不切换表达式,否则null
第一个表达式的值将始终强制计算第二个表达式.detach()
.
巧妙的代码唯一的问题是,一旦你喝完咖啡休息后回到它,它对你来说也没有任何意义.所以这更好:
if(the_styles) {
the_styles.appendTo('head')
the_styles = null;
}
else {
the_styles = the_styles.detach('.stylesheet');
}
Run Code Online (Sandbox Code Playgroud)
对我来说,即使是上面简单的版本也没有任何意义.在什么部分是显而易见的,但为什么是它这样做?
归档时间: |
|
查看次数: |
12817 次 |
最近记录: |