JavaScript 中的三元运算符具有多个表达式?
the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach();
显然,这是无效的。注意“;”在 appendTo()
和 the_styles=null
之间。我如何将它写在 1 行上并且仍然有多个这样的表达式?
the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach();
Obviously, this isn't valid. Notice the ";" between the appendTo()
and the_styles=null
. How do I write it on 1 line and still have multiple expressions like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以这种方式使用逗号运算符:
以下是 Mozilla 开发人员中心关于逗号运算符的描述:
Use the comma operator this way:
Here's what the Mozilla Developer Center writes about the comma operator:
谁需要三元运算符?
必须切换表达式,否则第一个表达式的
null
值将始终强制对第二个表达式.detach()
进行求值。聪明的代码唯一的问题是,一旦你在喝咖啡休息后回来看它,它甚至对你来说也没有任何意义。所以这要好得多:
对我来说,即使是上面的简单版本也没有任何意义。 什么部分很明显,但是为什么要这样做呢?
Who needs the ternary operator?
Had to switch the expressions around as otherwise the
null
value of the first expression will always force the second expression.detach()
to be evaluated.The only thing about clever code is that once you come back to it after a coffee break, it won't make any sense even to you. So this is much better:
To me, even the above simplistic version doesn't make any sense. The what part is obvious, but why is it doing that?
只需将代码块包装在
(function() {
和})()
中即可。现在是最困难的部分:为什么你想这样做?也许有更好的解决方案!
Just wrap the code block in
(function() {
and})()
.Now for the hard part: why would you want to do this? Perhaps there's a better solution!
我同意glowcoder,但如果你仍然想要它:
i agree with glowcoder but if you still want it:
如果您覆盖它,则不需要将其清空!
you dont need to null it if your overwriting it !