Nested objects with Javascript using prototype

P

p.tilhoo

Hello there,

I am a programmer mostly using c# and just started using Javascript.

I am trying to use prototype to declare and use an object and having
difficulties in coding for nested objects.

Basically my object called layercontrol will contain and array of
layergroup which in turn contains map layer objects. The layergroup
and layer object should be just like any object of properties and
methods.

I should be able to access each layergroup in my object and within
each layergroup, I should be able acess each layer object.

the concept is simple, but am jut struggling !!

Any help from you guys is most welcome.

look forward to hearing from you.

Thank you very much in advance.
 
R

RobG

Hello there,

I am a programmer mostly using c# and just started using Javascript.

I am trying to use prototype

If you are talking about the general purpose (and confusingly named)
library called Prototype.js, do not use it if your intention is to
learn javascript. If you get bogged down in its Class.create function
you'll be learning a pattern peculiar to Prototype.js and not
particularly useful outside it.

to declare and use an object and having
difficulties in coding for nested objects.

If you want to declare a nested object, you can use an object literal:

var obj = {outer:
{innerString: 'hey', func: function(){}
}
};

Which declares a single object with one property called outer. It's
value is a reference to another object with two properties: one called
innerString with a value that is a string and another called func
whose value is a refernce to a function.

Objects in javascript are essentially bundles of named properties.
There are a few rules on what you can use for names, the properties
can be pretty much anything including primitives, other objects,
arrays, functions and so on.

Why not post a bit more about what you are trying to do and what
you've tried so far?

Basically my object called layercontrol will contain and array of
layergroup which in turn contains map layer objects. The layergroup
and layer object should be just like any object of properties and
methods.

Hmm, don't have much time to help you here but try the pattern here:

<URL: http://www.crockford.com/javascript/private.html >


Of course there are many ways to approach a solution, that is just
one.

I should be able to access each layergroup in my object and within
each layergroup, I should be able acess each layer object.

You can use closures to reference the "private" parts and use public
or privileged functions as getters and setters. Have a go at a simple
case and let's see what comes up. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top