the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach();显然,这是无效的。注意appendTo()和the_styles=null之间的";“。我如何在一行代码中使用多个这样的表达式呢?
发布于 2010-07-03 02:32:56
按如下方式使用逗号运算符:
the_styles ? (the_styles.appendTo('head'), the_styles=null) : the_styles = $('.stylesheet').detach();以下是Mozilla开发人员中心对逗号操作符的描述:
如果要在需要单个表达式的位置包含多个表达式,则可以使用逗号运算符。此运算符最常见的用法是在for循环中提供多个参数。
发布于 2010-07-03 02:41:08
谁需要三元运算符?
the_styles = !the_styles && $('.stylesheet').detach() ||
the_styles.appendTo('head') && null;Had必须交换表达式,否则第一个表达式的null值将始终强制计算第二个表达式.detach()。
关于聪明代码的唯一一件事是,一旦你在喝完咖啡后回到它上面,它甚至对你来说也没有任何意义。所以这要好得多:
if(the_styles) {
the_styles.appendTo('head')
the_styles = null;
}
else {
the_styles = the_styles.detach('.stylesheet');
}对我来说,即使是上面的简单化版本也没有任何意义。什么部分是显而易见的,但是为什么要这样做?
发布于 2010-07-03 02:18:06
the_styles ? (function() {the_styles.appendTo('head'); the_styles=null})() : <etc>只需将代码块包装在(function() {和})()中即可。
现在是最难的部分:,为什么你想要这样做?也许有更好的解决方案!
https://stackoverflow.com/questions/3168136
复制相似问题