By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

Nested objects with Javascript using prototype

P: n/a
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.
Aug 31 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sep 1, 8:26*am, "p.til...@googlemail.com" <p.til...@googlemail.com>
wrote:
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. :-)
--
Rob
Sep 1 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.