More fun with undefined
T.J. Crowder
tj at crowdersoftware.com
Thu Jun 14 15:10:56 PDT 2012
Making a point of making this a separate thread from the current ??
and ??=thread(s), which are thankfully looking close to consensus. So
that's infix
and assignment.
Question: Should we consider unary as well?
I ask because I went searching through my code (and others') to see where
I'd get a lot of use out of ?? and ??=, and I will, but I also found a
**lot** of:
// 1
if (typeof foo === "undefined") { // Or foo === undefined
// ...
}
and
// 2
if (typeof foo !== "undefined") { // Or foo !== undefined
// ...
}
and
// 3
a = typeof foo !== "undefined" && foo;
...in my code, in jQuery, and in Prototype. jQuery also has a fair bit of
this (which I find slightly odd, but...):
// 4
a = typeof foo !== "undefined" && foo(arg);
Given ?? and ??=, is there a call for some unary form addressing the above?
Oddly, I think it would make the most sense if it were in the positive
(true if the argument _is_ undefined).
Just for illustration, I'll use £ as though it were an operator (because
obviously it won't be that, and worrying about spelling is something we
only need to do if the semantics are justified). The rule is simple:
&evaluates true if its argument is undefined, false otherwise:
// 1
if (£foo) {
// foo === undefined
}
and
// 2
if (!£foo) {
// foo !== undefined
}
and
// 3
a = !£foo && foo;
// a becomes foo if foo !== undefined
// a becomes false if foo === undefined
-- T.J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120614/0656364c/attachment.html>
More information about the es-discuss
mailing list