SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Location
    In the hearts of people who passionately hate me
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Global object undefined check method

    Hi all,

    I'm trying to create a global object to contain all my variables. I want to place a check to see whether my global variable name is taken. Like so:

    Code:
    if( typeof globalVar == "undefined"){
       globalVar = {};
    }
    else{
        // Check if 'globalVar1' is defined and then keep iterating.
    }
    If 'globalVar' is defined, check whether 'globalVar1' is defined. So, as you can see there are two questions from me.

    1) How do I check at runtime whether the variable 'globalVar1' exist?
    2) Even if the variable 'globalVar1' exist, how would I update the rest of my code to use the newly created 'globalVar1' global obj?

    Please help

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Adil K View Post
    1) How do I check at runtime whether the variable 'globalVar1' exist?
    2) Even if the variable 'globalVar1' exist, how would I update the rest of my code to use the newly created 'globalVar1' global obj?
    It's times like these that an array becomes of incredibly good use.

    Code javascript:
    if (!globalVar) {
    globalVar = [];
    }
    globalVar.push({...});

    That will add an object to the globalVar array regardless of whether globalVar exists or not.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jan 2009
    Location
    In the hearts of people who passionately hate me
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. What about a situation where an application has multiple js plugins and you're creating a plugin of your own? In this case, I want to check if up until this point any library has created a global variable (object, array, etc) with, let's say, 'globalVar'.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Is there any particular reason why your global object needs a name? If you are not going to reference it from anywhere else then you could use an anonymous global object which therefore wouldn't clash with any other object because it doesn't have a name to clash with.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Jan 2009
    Location
    In the hearts of people who passionately hate me
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks


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
  •