473,471 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to acess property of object/subobject...

Hi all,

Given the following:

var obj = new Object();
obj.prop1 = 1;
obj.prop2 = 2;
obj.subobj = new Object();
obj.subobj.prop3 = 3;

I'd like to automatically access the right property given it's name
like this:

var propName = whatever; // Can be "prop1", "prop2" or "prop3".
var propValue = obj["" or "subobj"][propName];

Technically, in the case of "prop1" or "prop2" the first square
brackets should disappear. Is there a way to do that?

Thanks!
Terry

Jul 23 '05 #1
1 2614
su**********@yahoo.com wrote:
Hi all,

Given the following:

var obj = new Object();
obj.prop1 = 1;
obj.prop2 = 2;
obj.subobj = new Object();
obj.subobj.prop3 = 3;

I'd like to automatically access the right property given it's name
like this:

var propName = whatever; // Can be "prop1", "prop2" or "prop3".
var propValue = obj["" or "subobj"][propName];

Technically, in the case of "prop1" or "prop2" the first square
brackets should disappear. Is there a way to do that?


Use a function to go down the list of accessors. The following expects
to be passed an object as arg 0, then each accessor in correct order:

function getValue(){
var x = arguments;
var i=0, v = x[i];
while ( x[++i] ) { v = v[x[i]]; }
return v;
}

// Test based on obj above
var w = 'prop1',
x = 'prop2',
y = 'subobj',
z = 'prop3';

alert(
w + ': ' + getValue(obj, w) + '\n'
+ y + '.' + z + ': ' + getValue(obj, y, z) + '\n'
+ 'obj.' + x + ': ' + obj[x]
);

If the accessors are a single string (say 'subobj.prop3') then use
split('.') to convert it to an array and proceed as above.
--
Rob
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
2
by: Dave | last post by:
Any thoughs on why a member subobject may be const but an inherited subobject may not? i.e. the following is not possible: class derived: public const base { .... }; but the following is:
6
by: bradb | last post by:
Hello, I have a textbox with an empty span element (place holder) next to it. When the user adds some text to the text box, I create a checkbox in the empty span element. When I create the...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
1
by: qwer | last post by:
hi im actually doing on a SMS application using Visual Basic.Net whereby the objective is to allow the user to send a SMS to the visual basic programme and i should decode out the hp no, car plate,...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
6
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
1
by: lokeshreddy16 | last post by:
'this is my code plz guys help how to save data from vb 2005 and other this that i am able to view the data from acess but i am not able save to acess i dont whether my code for save is correct of...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.