【发布时间】:2019-08-30 07:43:39
【问题描述】:
我看过这些行代码。
this.tween && this.tween.kill(),
this.tween = TweenMax.to(object, 1, {
...
})
这是简写吗
if(this.tween){
this.tween.kill();
}
this.tween = TweenMax.to(object, 1, {
...
})
谢谢 ;)
【问题讨论】:
-
有几件事要带走,注意
&&而不是&,if在可读性方面占上风,如果您最终扩展到多个语句块。跨度> -
不要压缩开发代码,直接用
if就行了。 -
我同意
&&的可读性不如if。这是一种“侧身Yo Yo problem”。您将阅读第一部分,查看 && 然后查看操作,您必须返回并阅读何时执行操作。这是因为当您开始阅读该行时不清楚它是否是有条件的。除非您正在做作业,否则这不是 JS 的惯用用法。它确实出现在 PHP 中,我认为 Ruby 和/或 PERL 如果无法建立连接,您可以使用像db.connect() OR die()这样的行来终止。
标签: javascript if-statement ampersand shorthand double-ampersand