[Javascript] Object Not Defined

Anthony Ettinger anthony at chovy.com
Fri Nov 11 12:12:52 CST 2011


var foo = function(a){return a && a.data.items || []};
foo(null);
foo({data: "bar"});
foo({data: { items: ["bar", "baz"]} });

On Fri, Nov 11, 2011 at 9:44 AM, Hassan Schroeder <hassan at webtuitive.com> wrote:
> On 11/11/11 4:45 AM, Terry Riegel wrote:
>
>> It would be nice if this did the same thing...
>>
>> function(a){return a.data.items || []};
>
> What makes you think it doesn't? That syntax seems to work fine for
> me in a quick test in a Chrome console:
>
>> var x = function(a){ return a || []; }
> undefined
>> x("foo")
> "foo"
>> x()
> []
>> x(null)
> []
>
> Or am I misunderstanding your goal?
>
> --
> Hassan Schroeder ----------------------------- hassan at webtuitive.com
> webtuitive design ===  (+1) 408-621-3445   === http://webtuitive.com
> http://about.me/hassanschroeder
> twitter: @hassan
>                          dream.  code.
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
>



-- 
Anthony Ettinger
http://anthony.ettinger.name
anthony at ettinger.name
+1 (831) 406-1123


More information about the Javascript mailing list