Wednesday, 15 May 2013

Existential conditional assignment operators in Coffeescript -


While reading the coffee script document, I was confused by the documentation states

? = ) can also be used for secure conditional assignments compared to = = , in cases where you may have to handle numbers or stars.

? What is the difference between operator and ?

and = completely different (but overlapping) conditions

|| Operator works the same way as it does in JavaScript, such as 0 and '' are false as far as || Regarding ; || Checks for validity in javascript sense.

What ? The operator is converted to javascript in == zero A? B is only b when a is null or undefined ; Coffee code means ? Definition of check.

Consider these: for [wrong, 0, '', blank, undefined, 6] do (V) - & gt; A = VA || = 'Pancakes' console.log ("# {v}} = 'pancakes':", a for v [false, 0, '', null, undefined, 6] do (v) - & gt; A = VA? = 'Pancakes' console.log ("# {v}? = 'Pancakes':", a)

The first loop will give you five pancakes and one 6 , The second loop gives you a false , 0 , '' , two pancakes and a 6 .

DEMO:

So if you only want JavaScript style behavior (i.e. 0 , false , and '' is to be ignored), then you probably want = = . If you want to leave only null and undefined , you want ? = .

No comments:

Post a Comment