Nested objects with Javascript using prototype

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • p.tilhoo@googlemail.com

    Nested objects with Javascript using prototype

    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.
  • RobG

    #2
    Re: Nested objects with Javascript using prototype

    On Sep 1, 8:26 am, "p.til...@googl email.com" <p.til...@googl email.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

    Comment

    Working...