Javascript: var myObject = myObject || {};

  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 164
  • Loc: Malaysia
  • Status: Offline

Post April 8th, 2011, 2:25 am

I would like to know why some people declare javascript vars like below:

Code: [ Select ]
var myObject = myObject || {};


I did some google but dint
get the correct keywords. Please advice. Thank You.
Malaysia > KL > TAR College
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 8th, 2011, 2:25 am

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post April 24th, 2011, 9:36 am

Are you sure it is Javascript?

C# has the following
Code: [ Select ]
return myObject = _myObject ?? (_myObject = new myObject());


all this does is returns the _myObject if it not null and if it is then it creates a new instance of myObject and returns it after assigning it to _myObject. Perhaps the Javascript is the same?

The way it reads is (your sample):
my object = my object or new object.

I have a sneak suspicion it might be doing the same thing
Watch me grow
  • baligena
  • Novice
  • Novice
  • No Avatar
  • Joined: Apr 15, 2011
  • Posts: 25
  • Status: Offline

Post May 2nd, 2011, 6:44 pm

when I name a variable like

var a = b || c ;

is so I can use it like this

//if true b or c equal 2
if(a == 2){document.write('either b or c equal 2')}

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 117 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.