gpt4 book ai didi

javascript - Coffeescript - 减少存在和不同的代码行?

转载 作者:行者123 更新时间:2023-12-02 16:34:51 26 4
gpt4 key购买 nike

在 Coffeescript 中,这些 ifunless 语句可以成为一行代码吗?

# 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 不是 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

关于javascript - Coffeescript - 减少存在和不同的代码行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27999382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com