||= is much needed?
Rick Waldron
waldron.rick at gmail.com
Thu Jun 14 13:04:14 PDT 2012
On Thu, Jun 14, 2012 at 2:39 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:
>
> snip
>
> I guess my concern is that there are significant existing subsystems where
> null is distinguished from undefined or where null has a specifically
> defined meaning that does not apply to undefined. For example:
>
> [[Prototype]] manipulation (Object.create, Object.getPrototypeOf,
> __proto__)
> RegExp exec and derived APIs
> JSON stringify/parse
> the DOM (WebIDL, provides lots of options vis [TreatUndefinedAs]] to
> explicitly control the mapping of undefined)
>
More examples... event objects created by DOM APIs use null as the initial
value of any data property whose value won't be known until an actual event
has occurred.
See:
http://www.w3.org/TR/webmessaging/
http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120614/
> snip
>
> One way to have it both ways is to have multiple syntactic forms for
> default value initializers. EG:
>
> function f(a = 1, b ??= 2, c ||= 3) { } //assuming ??= is undefined or
> null defaulting guard and ||= is falsy
>
> I'm not particularly convinced that the additional complexity is warranted
> but it would place the choice into ES programmers hands rather us trying to
> anticipate the typical intent and disadvantaging the untypical.
>
I agree with this entirely.
||= is complementary to || and makes sense - developers will embrace this
as is.
?? and ??= seem like "something is unknown" and unknown things can
otherwise be described as "undefined". Definitively, |null| is intentional
-- which implies something "known" and therefore cannot qualify as
"undefined". I think sticking to undefined will help to fix the abused "==
null" patterns in extant code (I'm thinking in the long term of course)
If null testing is needed:
a = a != null ? a : default;
I realize that the strawman currently spells it "?:", consider this message
an opposition to that.
Rick
>
> Allen
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120614/5a21f940/attachment.html>
More information about the es-discuss
mailing list