Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14715

Re: what does var adc = adc || {}; mean?

From Mike Winter <usenet@michael-winter.me.invalid>
Newsgroups comp.lang.javascript
Subject Re: what does var adc = adc || {}; mean?
References <4c004bc9-7ec9-41f5-ad85-063688237033@googlegroups.com>
Message-ID <b2HGr.362371$2l2.149922@fx01.am4> (permalink)
Organization virginmedia.com
Date 2012-06-27 18:04 +0100

Show all headers | View raw


On 27/06/2012 15:59, von kristoff wrote:

> var adc = adc || {};
>
> im building an object .. but why is the logical or there? any insite?!
>
> is it 'create adc' but if adc exists - then use adc .. ?

To an extent, but the variable declaration seems rather nonsensicle: if 
the variable needs a declaration it's going to always be initially 
undefined, and if it does exist then the declaration is unneeded.

The idiom can be used to effect default parameters in functions. It is 
also used with intrinsic event handlers where the browser passes an 
event object in some instances (e.g. Firefox) but is global in others 
(e.g. Internet Explorer). For example:

   elementRef.onclick = function (e) {
     e = e || event;
     // ...
   };

As for the mechanism, the logical OR operator evaluates the left-hand 
operand: if that value would type-convert to boolean true, the result of 
the expression is the value of that operand. Otherwise the result of the 
expression is the value of the right-hand operand. That is:

   x = a || b;

is a shorter way of writing:

   x = a ? a : b;

or even:

   if (a)
     x = a;
   else
     x = b;

Kind regards,
Mike

-- 
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

what does var adc = adc || {}; mean? von kristoff <mrjcnicolas@gmail.com> - 2012-06-27 07:59 -0700
  Re: what does var adc = adc || {}; mean? Mike Winter <usenet@michael-winter.me.invalid> - 2012-06-27 18:04 +0100
  Re: what does var adc = adc || {}; mean? Gregor Kofler <usenet@gregorkofler.com> - 2012-06-27 19:28 +0200
    Re: what does var adc = adc || {}; mean? Gene Wirchenko <genew@ocis.net> - 2012-06-27 14:17 -0700
      Re: what does var adc = adc || {}; mean? Gregor Kofler <usenet@gregorkofler.com> - 2012-06-28 00:32 +0200
        Re: what does var adc = adc || {}; mean? Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:22 -0700
        Re: what does var adc = adc || {}; mean? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-30 10:54 +0200
  Re: what does var adc = adc || {}; mean? von kristoff <mrjcnicolas@gmail.com> - 2012-06-27 11:46 -0700
  Re: what does var adc = adc || {}; mean? Scott Sauyet <scott.sauyet@gmail.com> - 2012-06-28 06:53 -0700

csiph-web