On Apr 14, 2011, at 5:55 PM, John J. Barton wrote:
> I guess the typical form is
> context.eventsMonitored = context.eventsMonitored || [];
JS hackers don't like assigning over top of the same value, so I find the
parenthesized assignment on the right of || more common.
> I'm not arguing "for" anything, only pointing out cases where undefined
> tests, which are pervasive in JS, require repeating extra repeating code.
Ok. It's good to have clarity over proposals. The infix-? idea Kyle put forth
was what you seemed to be following up with examples of your own. Indeed,
infix-? helps avoid parentheses:
!context.eventsMonitored ? context.eventsMonitored = [];
vs.
context.eventsMonitored || (context.eventsMonitored = []);
But that is all it does (net two chars saved above, thanks to loss of ! via ||
instead of &&), and it adds a dangling-else form.
>>> Temp introduced because `undefined` come at depth:
>>> var htmlPanel = context.getPanel("html", true);
>>> var vars = htmlPanel ? htmlPanel.getInspectorVars() : null;
>> Why wouldn't you use&& here?
> Sorry I don't understand the question. What the code wants to express is "if
> context.getPanel('html', true) is undefined
"is undefined" -> "is falsy". Does context.getPanel return null or undefined in
the not-found case?
> , set vars to null, otherwise set it to context.getPanel('html',
> true).getInspectorVars()".
If null or undefined will do, then my question was why not write this:
var htmlPanel = context.getPanel("html", true);
var vars = htmlPanel && htmlPanel.getInspectorVars();
Here is where Dmitry's proposal of ?. as a new operator, based on the same one
from CoffeeScript, can win:
var vars = context.getPanel("html", true)?.getInspectorVars();
Not the topic of the message you followed up directly (which was infix-?), but
no worries.
> Perhaps there is no better solution, but often I find that I want to say
> "call this chain of functions and use the bottom value, but if any of them
> return undefined, then just be undefined, don't get all pissed off and throw
> an exception. Being undefined is how JavaScript is." I was imagining that
> this was the feature being discussed.
No imagination required -- this is clearly the CoffeeScript ?. operator Dmitry
brought up. It has its uses. We should get back to a message whose subject is
explicitly about ?. ("Existential operator") and discuss it more.
/be
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss