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


Groups > comp.lang.javascript > #14708 > unrolled thread

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

Started byvon kristoff <mrjcnicolas@gmail.com>
First post2012-06-27 07:59 -0700
Last post2012-06-28 06:53 -0700
Articles 9 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#14708 — what does var adc = adc || {}; mean?

Fromvon kristoff <mrjcnicolas@gmail.com>
Date2012-06-27 07:59 -0700
Subjectwhat does var adc = adc || {}; mean?
Message-ID<4c004bc9-7ec9-41f5-ad85-063688237033@googlegroups.com>
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 .. ?

[toc] | [next] | [standalone]


#14715

FromMike Winter <usenet@michael-winter.me.invalid>
Date2012-06-27 18:04 +0100
Message-ID<b2HGr.362371$2l2.149922@fx01.am4>
In reply to#14708
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.

[toc] | [prev] | [next] | [standalone]


#14716

FromGregor Kofler <usenet@gregorkofler.com>
Date2012-06-27 19:28 +0200
Message-ID<jsffse$fkg$1@dont-email.me>
In reply to#14708
Am 2012-06-27 16:59, von kristoff meinte:
> 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 .. ?

Sort of. If adc evaluates to false (boolean false, undefined, 0, ...) 
adc is initialized as empty object, otherwise adc keeps its value or 
reference.

Gregor




[toc] | [prev] | [next] | [standalone]


#14724

FromGene Wirchenko <genew@ocis.net>
Date2012-06-27 14:17 -0700
Message-ID<gntmu7le41drms3tsmis7be1err2hflnmi@4ax.com>
In reply to#14716
On Wed, 27 Jun 2012 19:28:46 +0200, Gregor Kofler
<usenet@gregorkofler.com> wrote:

>Am 2012-06-27 16:59, von kristoff meinte:
>> 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 .. ?
>
>Sort of. If adc evaluates to false (boolean false, undefined, 0, ...) 
>adc is initialized as empty object, otherwise adc keeps its value or 
>reference.

     Because adc is being declared in that very statement, how could
it have a value other than undefined in the assignment expression?

Sincerely,

Gene Wrichenko

[toc] | [prev] | [next] | [standalone]


#14729

FromGregor Kofler <usenet@gregorkofler.com>
Date2012-06-28 00:32 +0200
Message-ID<jsg1lb$25r$1@dont-email.me>
In reply to#14724
Am 2012-06-27 23:17, Gene Wirchenko meinte:
> On Wed, 27 Jun 2012 19:28:46 +0200, Gregor Kofler
> <usenet@gregorkofler.com> wrote:
>
>> Am 2012-06-27 16:59, von kristoff meinte:
>>> 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 .. ?
>>
>> Sort of. If adc evaluates to false (boolean false, undefined, 0, ...)
>> adc is initialized as empty object, otherwise adc keeps its value or
>> reference.
>
>       Because adc is being declared in that very statement, how could
> it have a value other than undefined in the assignment expression?

(Re-)declaring a variable within a certain scope doesn't change or reset 
it's value.

var foo = 10;

...

var foo = foo || 20;

console.log(foo); // 10


I suppose situations without the var are more frequent:

function foo(bar) {
   bar = bar || 42;
   ...
}

though a

function foo(bar) {
   var bar = bar || 42;
   ...
}

wouldn't do any harm.

Gregor

[toc] | [prev] | [next] | [standalone]


#14735

FromGene Wirchenko <genew@ocis.net>
Date2012-06-27 19:22 -0700
Message-ID<knfnu7drke31abmbuuamkpk2sqkt3mvn5e@4ax.com>
In reply to#14729
On Thu, 28 Jun 2012 00:32:11 +0200, Gregor Kofler
<usenet@gregorkofler.com> wrote:

>Am 2012-06-27 23:17, Gene Wirchenko meinte:

[snip]

>>       Because adc is being declared in that very statement, how could
>> it have a value other than undefined in the assignment expression?
>
>(Re-)declaring a variable within a certain scope doesn't change or reset 
>it's value.

     Thank you.

[snip]

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14781

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-06-30 10:54 +0200
Message-ID<1615269.NoscLsuIhr@PointedEars.de>
In reply to#14729
Gregor Kofler wrote:

> I suppose situations without the var are more frequent:
> 
> function foo(bar) {
>    bar = bar || 42;
>    ...
> }
> 
> though a
> 
> function foo(bar) {
>    var bar = bar || 42;
>    ...
> }
> 
> wouldn't do any harm.

… and

  function foo(bar) {
    if (!bar) bar = 42;
    ...
  }

(with or without Block statement) would make sense. 


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [next] | [standalone]


#14720

Fromvon kristoff <mrjcnicolas@gmail.com>
Date2012-06-27 11:46 -0700
Message-ID<d7fb44fe-d878-43ba-97cf-81e7cb2dcc34@googlegroups.com>
In reply to#14708
On Wednesday, June 27, 2012 3:59:48 PM UTC+1, 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 .. ?

thanks guys .. i like x = a || b; 
But it makes sense - i guess for what context you didnt see it in ..

[toc] | [prev] | [next] | [standalone]


#14747

FromScott Sauyet <scott.sauyet@gmail.com>
Date2012-06-28 06:53 -0700
Message-ID<e8d5378b-a04f-437e-9132-03a87c830033@t20g2000yqn.googlegroups.com>
In reply to#14708
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 .. ?

Yes.  One reasonable usage is for a namespace object.  Code in
multiple different files might declare properties of the object `adc`,
but different combinations of them might be included on different
pages.  Without any module loader, this will allow you to avoid having
one file overwrite the global `adc` variable from another.

For example:

    file: util.js:
    -------------
    var MyApp = MyApp || {}
    MyApp.util = // define a number of utility functions

    file: dom.js:
    -------------
    var MyApp = MyApp || {}
    MyApp.dom = // define all DOM-related functions

    file: ajax.js:
    --------------
    var MyApp = MyApp || {}
    MyApp.ajax = // define all XHR-related functions

    file: numerics.js:
    ------------------
    var MyApp = MyApp || {}
    MyApp.numeric = // define all number-crunching functions

Now one page can include util.js and numeric.js and another can
include util.js, dom.js, and ajax.js, both without worrying about
order of inclusion.  Although both need to have MyApp declared and
defined, it will be done appropriately, regardless of the order they
are loaded.

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web