的实现下面的更简洁/优雅的方式,任何人都知道?
A = B if B?
谢谢。
编辑:
我在寻找引用A和B只有一次的解决方案。 并会向编译
if (typeof B !== "undefined" && B !== null) { A = B; }
或者别的类似。
有这短短的帮助有以下可读性更强一点:
someObject[someAttribute] = (someOtherObject[someOtherAttribute] if someOtherObject[someOtherAttribute]?)
这是我的问题的动机。
你可以说:
a = b ? a
例如,这样的:
a = 11
a = b ? a
console.log(a)
b = 23
a = b ? a
console.log(a)
会给你11
和23
在控制台(演示: http://jsfiddle.net/ambiguous/ngtEE/ )
也许是这样的:
A=_ if (_=B)?
扩展:
if ((_ = B) != null) {
A = _;
}
这将覆盖A和什么都在B,但只有当它不为空,引用都只有一次。
不知道咖啡脚本,但你可以使用或经营本在常规的JavaScript像这样:
a = b || a
文章来源: Most concise way to assign a value from a variable only if it exists in CoffeeScript?