Anyone knows of a more concise/elegant way of achieving the following?
A = B if B?
Thanks.
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.
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.
In JavaScript, before using a variable, we need to declare and initialize it (assign value). Unlike JavaScript, while creating a variable in CoffeeScript, there is no need to declare it using the var keyword. We simply create a variable just by assigning a value to a literal as shown below.
Automatically Global If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. This code example will declare a global variable carName , even if the value is assigned inside a function.
If you assign a value to a variable that you have not declared with var , JavaScript implicitly declares that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function.
if (typeof MyVariable !== "undefined" && MyVariable !==
You could say:
a = b ? a
For example, this:
a = 11
a = b ? a
console.log(a)
b = 23
a = b ? a
console.log(a)
will give you 11
and 23
in the console (demo: http://jsfiddle.net/ambiguous/ngtEE/)
Maybe something like:
A=_ if (_=B)?
expanded:
if ((_ = B) != null) {
A = _;
}
This will overwrite A with what ever is in B, but only if it is not null, referencing both only once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With