Javascript: var myObject = myObject || {};

  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Posts: 164
  • Loc: Malaysia

Post 3+ Months Ago

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.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

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
  • baligena
  • Novice
  • Novice
  • baligena
  • Posts: 25

Post 3+ Months Ago

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 85 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.