The method that Josh posted is (in some people's opinion) the
preferred method for assigning values, and is sometimes called the
'default pattern' for obvious reasons (the other common form is the
guard pattern &&). It, along with the ternary, is usually more
succinct than if/else, the latter being better suited to logic flow.

On Feb 18, 1:24 pm, Ricardo Tomasi <ricardob...@gmail.com> wrote:
> or simply if (!Obj.sortby) Obj.sortby = 'time'; it's a bit more
> efficient. All of undefined, null, 0 or "" will evaluate to false,
> there's no need to check for each of them.
>
> Anyway, Alexandre, the ternary you posted should also work, there is
> probably something else wrong in your code.
>
> - ricardo
>
> On Feb 18, 3:52 pm, "Josh Nathanson" <joshnathan...@gmail.com> wrote:
>
> > I think this might work, give it a try:
>
> > Obj.sortby = Obj.sortby || 'time';
>
> > -- Josh
>
> > -----Original Message-----
> > From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On 
> > Behalf Of Alexandre Plennevaux
> > Sent: Wednesday, February 18, 2009 9:07 AM
> > To: Jquery-en
> > Subject: [jQuery] default value of an object's property
>
> > hi there,
>
> > a quick question: i'm using an object to pass a series of variables.
> > Sometimes, i test for a specific property, which might not exist.
> > I can't seem to find the right way, i tried
>
> >  Obj.sortby = (typeof Obj.sortby == 'undefined' || Obj.sortby==''
> > ||Obj.sortby == null) ? 'time' : Obj.sortby;
>
> > but everytime that property "sortby" hasn't been set, firebug returns
> > it as "null"
>
> > can you clear that up for me?
>
> > Thanks a lot!
>
> > Alexandre

Reply via email to