Solved

Javascript question

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
SVG and IE11 2 17
Link Anchor Not working 9 46
replacing inline javascript with jquery 4 44
Echo'd values in dropdowns 6 28
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 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)

735 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