AFAIK, JS delays casting values to types until they are needed (for example, a Boolean context for an if statement's condition).

The || operator does not return a Boolean value; it returns the first value if it would evaluate to true in a Boolean context -- otherwise it returns the second value. These semantics ensure that you can use || in this way without breaking any of the assumptions you'd make about Boolean values. So the extra set of parentheses doesn't change the value.

Note that all of the traditional Boolean OR semantics still apply if you evaluate the resulting _expression_ as Boolean:

{a: 3} || {}   // ==> {a: 3} (would evaluate to true in Boolean context)
null   || {}   // ==> {}     (would evaluate to true)
null   || 0    // ==> 0      (would evaluate to false)

Someone who knows more about the JS type system can correct me on some of the finer points.

If you use Firefox, you can grab FireBug (http://www.joehewitt.com/software/firebug/) for an awesome JS console where you can play around with expressions like these.

Brad

On Oct 13, 2006, at 11:06 AM, Jerod Venema wrote:

You are correct in that if the first part of the statement is true, the rest of the statement gets short-circuited (no need to check if the second part of an OR is true if the first part is definately true). _javascript_ evaluates the OR statement first, so if the null gets seen, it takes the second part of the statement as the argument. I'm not sure about this (never tried it), but I would guess that if you had:

...
 }, (options || {}) );
...
(notice the extra set of parentheses)

you would pass the VALUE true/false, since the _expression_ would be evaluated, and the result then would be passed into the function.

Can anyone confirm that for me?

-Jerod

On 10/13/06, Bastian Flinspach <[EMAIL PROTECTED] > wrote:

Thank you Guys!

That really saves my day! Although i must admit, i'm still a bit
confused about the subject. I always thought, a logical _expression_ like
this would be evaluated and return true or false. Or is this bevause of
when the interpreter finds the first part to be true, it doesn't bother
with the rest, so it won't use the {}?
Ah, anyway, thanks for your help. My sanity is save for the moment and
i got something to puzzle out for the weekend.

best regards
Bastian




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs
-~----------~----~----~----~------~----~------~--~---

Reply via email to