仅当变量存在于 CoffeeScript 中时才从变量中分配值的最简洁方法? [英] Most concise way to assign a value from a variable only if it exists in CoffeeScript?

查看:11
本文介绍了仅当变量存在于 CoffeeScript 中时才从变量中分配值的最简洁方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道实现以下目标的更简洁/优雅的方式吗?

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 to
if (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)​

将在控制台中为您提供 1123(演示:http://jsfiddle.net/ambiguous/ngtEE/)

will give you 11 and 23 in the console (demo: http://jsfiddle.net/ambiguous/ngtEE/)

这篇关于仅当变量存在于 CoffeeScript 中时才从变量中分配值的最简洁方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆