最简洁的方法是从一个变量赋值一个值,只要它存在于CoffeeScript中? [英] Most concise way to assign a value from a variable only if it exists in CoffeeScript?
问题描述
任何人都知道实现以下操作的更简洁/优雅的方式?
Anyone knows of a more concise/elegant way of achieving the following?
A = B如果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 to
if (typeof B !== "undefined" && B !== null) { A = B; }
or something else similar.
有这个短的帮助有以下更好的可读性:
someObject [someAttribute] =(someOtherObject [someOtherAttribute]如果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/ )
这篇关于最简洁的方法是从一个变量赋值一个值,只要它存在于CoffeeScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!