SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Object 'location' Pointer?

    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 i 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!

  2. #2
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi again. So I find my problem was to do with pointer semantics which js doesn't have. Is there an alternative?

    For example, it would be convenient if I could do this:
    Code:
    function objBuild{
      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.

    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.

  3. #3
    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)
    Don't pass the obj object and you will be able to make changes to it that live beyond the scope of the function.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply.

    Not sure I understand. Wouldn't passing the iterated obj[x] as a parameter have the same effect as storing it globally.

    I need to iterate a path through the objects objects (...) then finally store data when an empty location is met. Iteration doesn't work in this way, iteratively storing obj[x] of obj[x] (...) does not allow me to write to the iterated original object.

  5. #5
    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)
    When a variable is passed, it's a copy of that variable that the function works with.
    Don't pass the variable to make permanent changes to it.

    Code javascript:
    function foo (a) {
        a = 'foo';
    }
    function bar () {
        a = 'bar';
    }
    var a = '';
    foo(a);
    // a doesn't equal 'foo', it equals ''
    bar(a);
    // a equals 'bar';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •