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?
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.
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.
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
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);
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:
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.
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)