On 4/14/2011 8:17 AM, Brendan Eich wrote:
On Apr 14, 2011, at 5:03 PM, John J. Barton wrote:

On 11:59 AM, Kyle Simpson wrote:
Brendan, you've asked for other coding examples where I use the pattern of some 
variable being `undefined` or not to trigger different behavior (that is, to 
use the variable or not). Here's two more:
Here are some examples from Firebug code:

Lazy object construction. A pervasive pattern in our code.

        if (!context.eventsMonitored)
            context.eventsMonitored = [];
This example does not argue strongly for infix-? given how JS users already write 
if-then (no else) or use || and&&, e.g.

     !context.eventsMonitored&&  (context.eventsMonitored = []);
     context.eventsMonitored || (context.eventsMonitored = []);
I guess the typical form is
            context.eventsMonitored =  context.eventsMonitored || [];

I'm not arguing "for" anything, only pointing out cases where undefined tests, which are pervasive in JS, require repeating extra repeating code.

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, set vars to null, otherwise set it to context.getPanel('html', true).getInspectorVars()".

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.

jjb
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to