Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2012, 02:01 PM   PM User | #1
TheIntern
New Coder

 
Join Date: Jan 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
TheIntern is an unknown quantity at this point
Object 'Exists' function

I am tasked with making a function that can accept many different items and return whether or not they exist.

Currently I am struggling on seeing if an object that holds all the methods for a button exists. I thought I could pass in the string name of the object then do a $('buttonObject').length >0 in the 'Exists' method to see if it was created yet. However this will never return true, and I cannot pass in the object into my exists function without it being created yet (throws error). So something like Exists(buttonObject) won't work, I'll have to pass in the string name of it (Exists('buttonObject')) or something else (I'm new to JS).

So how do I go about testing whether or not it exists if I cannot pass in buttonObject, without quotes?
TheIntern is offline   Reply With Quote
Old 01-31-2012, 03:09 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
As my crystal ball is under heavy maintenance, you'll have to show us some code for your "buttonObject". It exists as soon as you created it, otherwise it will be "undefined" or "null" depending on your initialisation.
devnull69 is offline   Reply With Quote
Old 01-31-2012, 03:34 PM   PM User | #3
TheIntern
New Coder

 
Join Date: Jan 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
TheIntern is an unknown quantity at this point
I can't, thats against company policy. And I know it won't exist until I create it, that is why I was asked to create an Exist method to prevent the use of an non-existant object.

What I can say is that the code looks like:

ButtonObject = {
MakeTheButton: function(){code}
TheClickMethod: function(){code}
}

I need to know if the ButtonObject exists yet by passing it, or its name into a function I made. However if I try Exists(object) it breaks as it passes by reference, so as of now I think I can only pass its name as a string.

I did find out about eval('Theobject'), but it looks like most people don't like it, that and if the object doesnt exist it throws a script error of undefined. Which is weird...but anyway
TheIntern is offline   Reply With Quote
Old 01-31-2012, 03:42 PM   PM User | #4
mjy
New Coder

 
Join Date: Jan 2012
Location: United States
Posts: 28
Thanks: 0
Thanked 6 Times in 6 Posts
mjy is an unknown quantity at this point
Although I don't understand the details of what you're doing, it seems that your basic problem is that in JavaScript you can't reference a non-existent data item (e.g. pass it to an 'exists()' function) without triggering a runtime error. One workaround you might experiment with is to preface the object name with 'window.'. Provided that you're working in a browser environment, global variables are actually properties of the global 'window' object. Referencing a non-existent property of an object merely returns 'undefined' and doesn't generate a runtime error. The following global code (not inside a function) illustrates this point:
Code:
var x = 5;

/* displays 5: */
alert (x);

/* also displays 5: */
alert (window.x);

/* y has NOT been declared, i.e. it doesn't exist: */

/* just displays "undefined": */
alert (window.y);

/* causes a runtime error: */
alert (y);
__________________
Author of the Kindle book JavaScript: Just the Basics - A Primer for the Complete Beginner. Learn JavaScript for the price of a fancy coffee drink.
mjy is offline   Reply With Quote
Users who have thanked mjy for this post:
TheIntern (01-31-2012)
Old 01-31-2012, 03:49 PM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
being that we're in the realm of pseudo code, wouldn't this work:

Code:
if (!ButtonObject) {
ButtonObject = {
MakeTheButton: function(){code}
TheClickMethod: function(){code}
} else {
somethingElse()
}
xelawho is offline   Reply With Quote
Old 01-31-2012, 03:50 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
If you need to give buttonObject as a string (I don't understand why) you can also do this
Code:
var myobject = "buttonObject";
if(window[myobject]) {
   // buttonObject exists here
}
devnull69 is offline   Reply With Quote
Old 01-31-2012, 03:56 PM   PM User | #7
mjy
New Coder

 
Join Date: Jan 2012
Location: United States
Posts: 28
Thanks: 0
Thanked 6 Times in 6 Posts
mjy is an unknown quantity at this point
Another way to deal with an object that might not exist, and avoid a runtime error, is to use JavaScript's try/catch mechanism. Say for example that you have defined an 'exists(obj)' function. The function can test whether the object it is passed is 'null' or 'undefined' but if you pass a totally non-existent (undeclared) object, a runtime error will happen before the function even runs. Here's a way to deal with that:
Code:
var objExists;
try
   {
   objExists = exists (dubiousObject);
   }
catch (e)
   {
   objExists = false;
   }

alert (objExists);
__________________
Author of the Kindle book JavaScript: Just the Basics - A Primer for the Complete Beginner. Learn JavaScript for the price of a fancy coffee drink.
mjy is offline   Reply With Quote
Old 01-31-2012, 04:11 PM   PM User | #8
TheIntern
New Coder

 
Join Date: Jan 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
TheIntern is an unknown quantity at this point
Quote:
Originally Posted by mjy View Post
Although I don't understand the details of what you're doing, it seems that your basic problem is that in JavaScript you can't reference a non-existent data item (e.g. pass it to an 'exists()' function) without triggering a runtime error. One workaround you might experiment with is to preface the object name with 'window.'. Provided that you're working in a browser environment, global variables are actually properties of the global 'window' object. Referencing a non-existent property of an object merely returns 'undefined' and doesn't generate a runtime error. The following global code (not inside a function) illustrates this point:
Code:
var x = 5;

/* displays 5: */
alert (x);

/* also displays 5: */
alert (window.x);

/* y has NOT been declared, i.e. it doesn't exist: */

/* just displays "undefined": */
alert (window.y);

/* causes a runtime error: */
alert (y);
Thanks, this works as it just passes in 'undefined', although they still want me to try and make it more generic I think, (passing in just the name).

As for the !ButtonObject, they want to make it modular and generic, while also teaching me JS so I can't do that.

As for the try catch, they don't use any in the gigantic enterprise app that this is, I guess the mindset is you shouldnt have any errors what so ever to be caught..ever.

and again for doing window[buttonobject] not sure how that works as I haven;t tried it out, but they want a method made for it.
TheIntern is offline   Reply With Quote
Old 01-31-2012, 04:27 PM   PM User | #9
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Code:
function Exists(objname) {
   return (window[objname] !== undefined);
}
devnull69 is offline   Reply With Quote
Old 01-31-2012, 04:46 PM   PM User | #10
TheIntern
New Coder

 
Join Date: Jan 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
TheIntern is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
Code:
function Exists(objname) {
   return (window[objname] !== undefined);
}
Yea I just implemented that, except created two else if's one for if its true and one to return if its false as this checks more than just objects (things in arrays and all that)

Thanks for your help
TheIntern is offline   Reply With Quote
Reply

Bookmarks

Tags
error, exists, function, method, object

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:38 PM.


Advertisement
Log in to turn off these ads.