Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript question

Posted on 2013-01-11
4
Medium Priority
?
261 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 83

Accepted Solution

by:
leakim971 earned 668 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 668 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 664 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

782 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