Solved

Javascript question

Posted on 2013-01-11
4
257 Views
Last Modified: 2013-01-11
Hi,

Could any one explain the below code for me, I want to make sure that I understand it correctly.

More precisely:  var mystuff = {};  ???

Thanks!!!


if(typeof(mystuff) === "undefined" || !mystuff) {
    var mystuff = {};
}
if(typeof(mystuff.widget) === "undefined" || !mystuff.widget) {
    mystuff.widget = {};

Open in new window

0
Comment
Question by:Rad1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
ID: 38769239
// if mystuf variable if not defined/created OR if mystuff value is null
if(typeof(mystuff) === "undefined" || !mystuff) {
// create mystuf as an empty hash table
    var mystuff = {};
}
// if the hash table mystuf don't have a widget object(membre) OR if this object value is null
if(typeof(mystuff.widget) === "undefined" || !mystuff.widget) {
// set the widget element of mystuf as an empty hash table
    mystuff.widget = {};
0
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 167 total points
ID: 38769244
Just to complement previous post:

var mystuff = {};  

initialize the mystuff variable to a new empty object. the if logic only checks if this object has been defined previously.
Check:
http://stackoverflow.com/questions/7118575/init-object-in-javascript-using-operator
0
 
LVL 10

Assisted Solution

by:P1ST0LPETE
P1ST0LPETE earned 166 total points
ID: 38769317
Just to further the complement, and to clarify that the IF logic (while generally ok) does have some holes in it.

Concerning mystuff = { };

{ } is syntax for an object literal. Object literal syntax was introduced in JavaScript version 1.2 and is what has allowed for the creation of Json. The following produces the same result:

var mystuff = { };
var mustuff = new Object();


Concerning the IF logic

There are 6 falsy values in Javascript:
undefined, null, NaN, 0, false, and empty string.

So, mystuff can potentially be previously defined and it will still be set to a new object.  Example:

var mystuff = 0;
var mystuff = '';
var mystuff = 12 / 'abc';  //NaN
var mystuff = false;

In all 4 cases, mystuff will evaluate as false and be assigned as a new object in the following code:

if ( typeof(mystuff) === "undefined" || !mystuff ) {
    var mystuff = {};
}

However, most developers will not create a variable to hold a number or string and then (abuse JavaScript's loosely type variables) and assign it as an object.  So this is me just nitpicking.

Also, in general practice I would say that typeof(mystuff) === "undefined" in the IF statement above is over kill.  Simply doing the following will give you the same results:

if ( !mystuff ) {
    var mystuff = {};
}

Which can be shortened to one line:

mystuff = mystuff || { };
0
 

Author Closing Comment

by:Rad1
ID: 38769458
Thank you very kindly for the information!!!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question