Heya Tobie,
We should first discriminate where we actually need this, and where a
= a || default is sufficient--
and then decide if we need a shortcut (my guess is that we probably
want one).
I don't really like the $default or $d name. $D is better, but also
not that perfect.
Maybe we can add something to the Prototype object, like Prototype.K.
Don't really have a clue here.
I think the default value to assign should be optional, and default
to "null":
somethingSomething = function(value){
return value === undefined ? (arguments[1] === undefined ? null :
arguments[1]) : value;
}
btw, one of the most annoying things on the "evaluates false" side of
things for me is this:
>>> 1 || 'a'
1
>>> 0 || 'a'
"a"
In Ruby, this does what you think it should do:
irb(main):001:0> 1 || 'a'
=> 1
irb(main):002:0> 0 || 'a'
=> 0
Best,
Thomas
Am 19.02.2007 um 07:01 schrieb Tobie Langel:
>
> Hi all,
>
> I find myself writing code like that the whole time:
>
> example = example === undefined ? null : example;
>
> We could imagine the following utility function:
>
> $default = function(value, default) {
> return value === undefined ? default : value;
> }
>
> So that the above could be shortened to:
>
> example = $default(example, null);
>
> We could even decide to shorten it to $d:
>
> example = $d(example, null);
>
> I'm not 100% sure I like it... so I'm submitting it here for
> discussion before eventually posting a patch.
>
> Regards,
>
> Tobie
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype: Core" 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/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---