SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot SlaveToTheMan's Avatar
    Join Date
    Aug 2001
    Location
    Land of OZ
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check for object existence question

    Question on how to check for the existence of an object:

    I have a situation where I need to check if a JS variable has been set in the page. I wrote a function to check for the validity of an object, but I ran into several issues while trying to check for the existence of an object. To understand how to check for the existence of an object, a ran of the following script in the body of the HTML page. Basically, it checks for the existence of variable "z" which does not exist and is not declared in the page. I commented in and out each line that defines the "ans" variable and recorded the result for that line after the "-->". I also labeled each line as "v1", "v2", and so on.

    Code JavaScript:
    var isValidObj = (function(objToTest){
                    if (typeof objToTest === undefined) {
                        return false;
                    }
                    if (objToTest === null) {
                        return false;
                    }
                    return true;
                });
     
                //var ans = isValidObj(z); // v1 --> throws error
                //var ans = isValidObj(window.z); // v2 --> true
                //var ans = (z === undefined); // v3 --> throws error
                //var ans = (z); // v4 --> throws error
                //var ans = (window.z === undefined); // v5 --> true
                //var ans = (window.z); // v6 --> undefined
                //var ans = (typeof z === 'undefined'); // v7 --> true
     
                alert(ans);

    I think I understand the results of each line except for v1, v3, and v4. I don't understand why these throwing error specially since "window.z" works just fine. Shouldn't saying "z" in your code be the same as saying "window.z". Isn't the window object assumed? Does this mean that each time I check for an objects existence I need to use the fully qualified name of "window.TheObjectImChecking"? It is also puzzling to me why v7 works but nothing else can refer to z directly.

    Thanks in advance...

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    This can be why it may be worth looking in to using try/catch/finally when you know that an error may be thrown.


    Code javascript:
    var ans;
    try {
        ans = isValidObj(z);
    }
    catch (e) {
        ans = false;
    }
    finally {
        alert(ans);
    }

    The caught error can contain lots of useful info, for example:

    Code:
    {
        arguments: ["z"],
        type: 'not_defined',
        message: 'z is not defined',
        stack: 'Reference Error: z is not defined',
        name: 'ReferenceError'
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot SlaveToTheMan's Avatar
    Join Date
    Aug 2001
    Location
    Land of OZ
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that's an interesting solution. I guess this is an issue in JS because it isn't compiled except at runtime. A compiler would catch this type of issue. Is my understanding of the issue sound? Due to that, I guess it causes us to do some extra error checking which is unnecessary in Java. I was ultimately trying to create a reusable method similar to isNull() which I've used in Java a lot to check for null pointers.

    thanks, I'm also just trying to gain a deeper understanding of JS as a language and I don't see a lot of discussions on this in books or online.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    People trying to use techniques from unrelated languages

    JavaScript and Java do not have much in the way of compatible similarities.
    The World's Most Misunderstood Programming Language

    Perhaps the most eye-opening series of videos made about JavaScript are those by the Douglas Crockford called Crockford on JavaScript. If you have a few hours, or several, you won't regret it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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