问题描述
有人知道实现以下目标的更简洁/优雅的方式吗?
Anyone knows of a more concise/elegant way of achieving the following?
如果 B,A = B?
谢谢.
我正在寻找只引用 A 和 B 一次的解决方案.并且会编译成if (typeof B !== "undefined" && B !== null) { A = B;}
或其他类似的东西.
I'm looking for a solution that references A and B once only. And would compile toif (typeof B !== "undefined" && B !== null) { A = B; }
or something else similar.
这个简短的帮助使以下内容更具可读性:someObject[someAttribute] = (someOtherObject[someOtherAttribute] if someOtherObject[someOtherAttribute]?)
这就是我提出问题的动机.
To have this short helps have the following a bit more readable:someObject[someAttribute] = (someOtherObject[someOtherAttribute] if someOtherObject[someOtherAttribute]?)
That is the motivation for my question.
推荐答案
你可以说:
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/)
will give you 11
and 23
in the console (demo: http://jsfiddle.net/ambiguous/ngtEE/)
这篇关于仅当变量存在于 CoffeeScript 中时才从变量中分配值的最简洁方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!