Coffeescript - 减少存在和不相同的代码行?
Coffeescript - Reduce lines of code for presence and non-same?
在 Coffeescript 中,这些 if
和 unless
语句可以成为一行代码吗?
# ensure both variables are present
if var1 and var2
# ensure the variables are different
unless var1 is var2
# now do something!
我需要两个变量都存在并且两者不同。
只需在第一个if
中添加a is b
的补码,Javascript将从左边开始计算表达式(短路),它不会测试a isnt b
除非它们都被定义
# Your example boils down to this
if a and b and a isnt b
# Do something
# Why not Use a? to check if a is defined
if a? and b? and a isnt b
# Do something
在 Coffeescript 中,这些 if
和 unless
语句可以成为一行代码吗?
# ensure both variables are present
if var1 and var2
# ensure the variables are different
unless var1 is var2
# now do something!
我需要两个变量都存在并且两者不同。
只需在第一个if
中添加a is b
的补码,Javascript将从左边开始计算表达式(短路),它不会测试a isnt b
除非它们都被定义
# Your example boils down to this
if a and b and a isnt b
# Do something
# Why not Use a? to check if a is defined
if a? and b? and a isnt b
# Do something