Javascript Object 'location' Pointer?

Associate
Joined
26 Nov 2004
Posts
1,480
Location
Gran Canaria
Hi

It maybe I'm barking up the wrong tree here but i'll try and explain what I'm trying to do.

I'm trying to update an object, rather an element within an object, and would like to be able to store the location as a variable.

For example:

ref = Object[cat1][cat2][cat3];

I don't wish ref to store the contents of the referenced data with the object, rather the location within its structure. This would allow me to update the Object after some iteration.

Instead of:

Object[cat1][cat2][cat3] = data;

(which I've lost through further iteration)

ref = data;

I'm kinda tying myself in knots and would really like if someone could point me in the right direction. Thanks in advance!
 
It depends whether the data being stored there are value types or reference types. If they're reference types then the code you posted will work, but if they're value types then you'd need pointer semantics, which JavaScript doesn't have.
 
It depends whether the data being stored there are value types or reference types. If they're reference types then the code you posted will work, but if they're value types then you'd need pointer semantics, which JavaScript doesn't have.

Yep, youd have to make a copy of the object and then take the data out you need.
 
So still having this problem. Is there an alternative?

For example, it would be convenient if I could do this:
Code:
function objBuild (someIds){
  var st = '';
  for (var i in someIds){
     st = st + "[" + someIds[i] + "]";
  }
  obj+someIds = newData;
}
But of course this obj+someIds results in a string, rather than a reference to an object within an object, or object of object of object etc.

Alternately I considered:

Code:
function iter(obj, selCats, data){
  for (var x = 0; x < obj.length; x++){
    if (obj[x].id==selCats[x]){
      iter(obj[x], selCats, data);
      break;
    } 
  }
  obj[selCats[x]] = data;
}

But this would leave the original object untouched.

If this isn't clear please let me know and i'll try explain better.
 
Last edited:
I dont think how you are doing it will work, you need to make a new object to store the old data in so you can modify the first object and still have a copy of the old data.

Code:
  function copy_object( objectToCopy )
  {
    var newObject = new Object();

    for ( var element in objectToCopy )
    {
      newObject[element] = objectToCopy[element];
    }
    return newObject;
  }
 
Thanks for that!

Looks great. I think it needs some modification to work recursively but should be me on the right track. Cheers!
 
Back
Top Bottom