Solved

Javascript question

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now