Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #4
    mjy
    mjy is offline
    New Coder
    Join Date
    Jan 2012
    Location
    United States
    Posts
    28
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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.

  • Users who have thanked mjy for this post:

    TheIntern (01-31-2012)

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    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()
    }

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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
    }

  • #7
    mjy
    mjy is offline
    New Coder
    Join Date
    Jan 2012
    Location
    United States
    Posts
    28
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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.

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Code:
    function Exists(objname) {
       return (window[objname] !== undefined);
    }

  • #10
    New Coder
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •