[Javascript] Object Not Defined

Brian L. Matthews blmatthews at gmail.com
Fri Nov 11 12:15:43 CST 2011


On 11/11/11 9:44 AM, Hassan Schroeder 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?

The syntax works, but in the first case, if either a or a.data is 
undefined, it will result in a JavaScript error.

Brian


More information about the Javascript mailing list