SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Sep 2, 2008, 06:54 #1
- 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!
-
Sep 3, 2008, 02:54 #2
- Join Date
- Sep 2008
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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; }
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; }
If this isn't clear please let me know and i'll try explain better.
-
Sep 3, 2008, 17:18 #3
- Join Date
- Jan 2007
- Location
- Christchurch, New Zealand
- Posts
- 14,729
- Mentioned
- 104 Post(s)
- Tagged
- 4 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
-
Sep 4, 2008, 00:57 #4
- 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.
-
Sep 4, 2008, 17:22 #5
- Join Date
- Jan 2007
- Location
- Christchurch, New Zealand
- Posts
- 14,729
- Mentioned
- 104 Post(s)
- Tagged
- 4 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