www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to use variable in namespace

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319

    How to use variable in namespace

    Hi,

    Does anybody know how I can use a variable in a namespace?

    Code:
    var variable = 'bar';
    
    new GLOBAL.hi.there.variable();
    (above code returns an undefined)

    Thanks!

    Christophe

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Each "namespace" needs to be an object -- could be a generic object, function, DOM node, anything that'll hold properties.

    PHP Code:
    var com = {};
    com.google = {};
    com.google.www = {};
    com.google.www.search = function(s) {

      
    // since we're actually working in the context of an object,
      // we can refer to "this" to set static-ish variables, like a search history:
      
    if (!this.search_history) {
        
    this.search_history = [];
      }
      
    this.search_history.push(s);

      
    // perform the actual "search" ...
      
    alert("No results for " "! Try searching with svidgen.com instead!");
    // com.google.www.search()

    // you can then use "with"
    // even though you shouldn't generally be using "with" ...
    with (com.google.www) {
      
    search("for something");
      
    search("for something else");
    }

    // we should be able to see the search history in the
    // namespace now
    alert(com.google.www.search_history.length); 

  3. #3
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319
    I'm not sure if you understand me (or if I understand your reply)

    I try to achieve the following:

    Code:
    // I try to write some code like ...
    GLOBAL.foo.bar.variable
    
    // Instead of ...
    
    switch ( variable ) {
        case 'var1':
            GLOBAL.foo.bar.var1;
        break;
        case 'var2':
            GLOBAL.foo.bar.var2;
        break;
        case 'var3':
            GLOBAL.foo.bar.var3;
        break;
    }
    Hope this clears things out

    Christophe

  4. #4
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Yup. I'm pretty sure I didn't understand what you're trying to accomplish. But, given your example:

    Code:
    // I try to write some code like ...
    GLOBAL.foo.bar.variable
    
    // Instead of ...
    
    switch ( variable ) {
        case 'var1':
            GLOBAL.foo.bar.var1;
        break;
        case 'var2':
            GLOBAL.foo.bar.var2;
        break;
        case 'var3':
            GLOBAL.foo.bar.var3;
        break;
    }
    It looks like you're working with an existing GLOBAL.foo.bar object, and you want to refer to some semi-unknown property of that object. You have a variable (which you've called variable) that contains a string referring to the property. So, what I think you're looking for is this:

    Code:
    GLOBAL.foo.bar[variable];
    That'll get you the property of GLOBAL.foo.bar indicated by the string or number value stored in variable. I'd be interested to see more of your code and what problem this is solving. Could you post some of your code?

  5. #5
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319
    Using the array notation works fine for me! thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles