www.webdeveloper.com
Results 1 to 4 of 4

Thread: Dynamic switching between "root" and "sub" properties of a nobject

  1. #1
    Join Date
    Feb 2009
    Posts
    32

    Dynamic switching between "root" and "sub" properties of a nobject

    Code:
    obj.prop1
    obj.prop2.prop1
    obj.prop3.prop1
    I can easily dynamically switch between prop2 and 3:

    Code:
    var x = "prop2"
    return obj[x]["prop1"]
    
    var x = "prop3"
    return obj[x]["prop1"]
    Is there a way to do the same for obj.prop1? The following will not work, but should make clear what I intend to do:

    Code:
    switch(y)
    {
      case 0:
        var x = "";
        break;
      case 0:
        var x = "prop2";
        break;
      case 0:
        var x = "prop3";
        break;
    }
    
    //Should alert obj.prop1 if y was 0
    alert(obj[x]["prop1"]);
    I could make an exception:

    Code:
    if(y == 0){alert(obj["prop1"]);}
    else
    {
      switch(y)
      {
        //And so on
      } 
    }
    But that becomes cumbersome when I need more than just prop1 (e.g. car.wheel, car.color, car.whatever...).
    Any way to tackle this without having to resort to eval()?

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773

  3. #3
    Join Date
    Feb 2009
    Posts
    32
    Thanks, toicontien, but that is not the issue. The issue is that I also need to access obj.prop1 in the same way. Suppose I have this:

    Code:
    obj.prop1
    obj.prop2.prop1
    var x = 1
    
    function f1()
    {
      if(x == 1){return obj.prop1;}
        else{return obj.prop2.prop1;}
    }
    
    function f2()
    {
      if(x == 1){return obj.prop1;}
        else{return obj.prop2.prop1;}
    }
    Imagine if I have functions f1 through f100. Each time I have to check which value x is. It would be much easier to say:

    Code:
    obj.prop1
    obj.prop2.prop1
    var x = "prop2"
    
    function f1()
    {
      return obj[x]["prop1"];
    }
    
    function f2()
    {
      return obj[x]["prop1"];
    }
    But then how can I access obj.prop1? AFAIK, there is no value I can assign to x which would make obj.[x]["prop1"] result to obj.prop1.

    The only thing I can think of to keep this manageable is:

    Code:
    obj.prop1
    obj.prop2.prop1
    var x = 1
    var y = (x == 1) ? obj : obj.prop2;
    
    function f1()
    {
      return y.prop1;
    }
    However, I'm a bit concerned about possible memory/resource issues when creating copies of potentially large objects this way. So I was wondering if there was another way to do this.

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    Consider:
    Code:
    var a = {}; // An object
    var b = a;
    The variable "b" is actually a pointer to the object in the variable "a". In JavaScript, objects are accessed and passed by reference, so b = a will never result in a new object. Instead, "b" becomes a pointer to "a".

    I'm still not following what exactly you're trying to do. My gutt is telling me that what you are proposing isn't the best solution. What is "y" in your example? It seems from your first post that "y" is an integer. If "y" is an integer, why not use an array of objects? Can you explain more about why you want to do this?

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