Hunter Larco
Hunter Larco

Reputation: 780

Javascript change variable based on variable reference

In a program i am writing i need an object variable that looks like this:

var map {
    cube: {__pt3arraylocation__:[0,0,0], poly: new Object()},
    other: {__pt3arraylocation__:[1,0,0], poly: new Object()}
};

However, i want to be able to type map.cube and have it return the pt3arraylocation as a default unless i specify what i want by typing map.cube.poly

for example: map.cube would return [0,0,0] and map.cube.poly would return the object poly in the object cube

thanks in advance

Upvotes: 0

Views: 229

Answers (5)

KooiInc
KooiInc

Reputation: 122906

T.J. Crowder is right. Based on his answer I'd like to remark that you can also assign map.cube like this

var map = { 
             cube: function(){ 
                     var val = [0,0,0];
                     val.poly = {}; 
                     return val; }()
          };

Or for more values:

function polyval(val){
    val.poly = {}; 
    return val; 
}
var map = { 
            cube: polyval([0,0,0]),
            other: polyval([1,0,0]),
            more: polyval([1,1,0])
            /* ... etc */
};

Upvotes: 0

Imp
Imp

Reputation: 8609

There is no way to do that exactly as you want - if you request an object (which map.cube is), you get an object. However, there are a few ways to do something similar.

  • when used as a parameter to functions or operations that require string, like alert(map.cube) or "sometext" + map.cube, the object is converted to string by calling the toString function. You can therefore define, for example:

    map.cube.toString = function() { return this.__pt3arraylocation__.toString(); };
    
  • a similar thing happens when there if the object is used in context when a number is needed. In this case, it is converted with valueOf(). So you can use, for example

    map.cube.valueOf = function() { return this.__pt3arraylocation__.length; };
    
  • or you can obtain the default value via a function call, if you define your object as a function instead

    var map = {
       cube: function() {
           return map.cube.__pt3arraylocation__;
       }
    }
    
    map.cube.__pt3arraylocation__ = [0,0,0];
    map.cube.poly = new Object();
    
    alert(map.cube.__pt3arraylocation__); // 0,0,0
    alert(map.cube.poly);                 // [object Object]
    alert(map.cube());                    // same as map.cube.__pt3arraylocation__
    

    As you can see, in JavaScript, functions are objects like any other, so you can not only call them, but also set fields and methods to them.

Upvotes: 0

xCander
xCander

Reputation: 1337

That is not possible to achive. Maybe you can play around with toString()

var map = {
    cube: {
      __pt3arraylocation__:[0,0,0], 
      poly: new Object(), 
      toString : function() {
         return this.__pt3arraylocation__.toString()
      }
    },
    other: {__pt3arraylocation__:[1,0,0], poly: new Object()}
};

map.cube == '0,0,0'
map.cube.split(',') == map.cube.__pt3arraylocation__

Upvotes: 0

Kendall Frey
Kendall Frey

Reputation: 44326

The best way to do this I would say is like this:

var map {
    cube: [0,0,0],
    other: [1,0,0]
};
map.cube.poly = new Object();
map.other.poly = new Object();

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074305

i want to be able to type map.cube and have it return the pt3arraylocation as a default unless i specify what i want by typing map.cube.poly

for example: map.cube would return [0,0,0] and map.cube.poly would return the object poly in the object cube

You can't do that in JavaScript.

However, as an alternative, it's worth noting that you can add arbitrary properties to arrays if you want to. So for instance:

var map {
    cube:  [0,0,0],
    other: [1,0,0]
};
map.cube.poly = {};  // {} is the same as `new Object()` but not subject to people...
map.other.poly = {}; // ...overriding the `Object` symbol

Then map.cube gives you the array, and map.cube.poly gives you the object you've stored on that array.

How is this possible? Because in JavaScript, arrays aren't really arrays. They're just objects that have an automatic length property, treat a class of property names (all numeric ones) in a special way, and have Array.prototype backing them up. And of course, you can add properties to any object.

There's no literal syntax for doing this, which is why I had to do it with assignments after the object initializer above. But it's perfectly valid. I use it for cross-indexing arrays all the time.

Do be sure, if you do this, that you're not using for..in incorrectly; more.

Upvotes: 3

Related Questions