Solved

Javascript question

Posted on 2013-01-11
4
255 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
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 35

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

828 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