prototype-based vs class-based OOP langauges

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Morris

    #1

    prototype-based vs class-based OOP langauges

    I would like to know the benefits for javascripts' prototype objects as
    compared to say java class-based objects.
    The main points that I have on my mind are:

    1. Is there a way to mark properties in prototype-based langauges as 'final
    or 'private' as you can in java so that some properties are 'hidden' from
    child objects.

    2. Is there a way for objects to limit the delete function in javascript, or
    any prototype-based langauge, to prevent to object structure from been
    'broken'. ie if an object 'a' has a child object 'b', can object 'b' rely on
    the fact object 'a' provides the features the next time object 'b' is used.

    thanks in advance

    Neil Morris



    --
    my IE6 favorites for import into Netscape/Internet Explorer




  • Lasse Reichstein Nielsen

    #2
    Re: prototype-based vs class-based OOP langauges

    "Neil Morris" <Neilmorris@bti nternet.com> writes:
    [color=blue]
    > I would like to know the benefits for javascripts' prototype objects as
    > compared to say java class-based objects.[/color]

    The advantage is the ease of dynamic modification, something that
    isn't possible when your class structure is fixed at compile time.
    [color=blue]
    > The main points that I have on my mind are:
    >
    > 1. Is there a way to mark properties in prototype-based langauges as 'final
    > or 'private' as you can in java so that some properties are 'hidden' from
    > child objects.[/color]

    No. When you use an object as prototype, you can see all its properties.
    If you want to change it, you can set another property on the child.
    [color=blue]
    > 2. Is there a way for objects to limit the delete function in javascript, or
    > any prototype-based langauge, to prevent to object structure from been
    > 'broken'. ie if an object 'a' has a child object 'b', can object 'b' rely on
    > the fact object 'a' provides the features the next time object 'b' is used.[/color]

    Don't know about other languages, but not in Javascript.
    If you want to depend on a property, make it a property of yourself
    ("b.prop = b.prop;" will make the property of the prototype a property
    of b itself)>

    Some build-in properties cannot be deleted, but there is no way to set
    that on user defined properties.

    I must admit that I don't like the way delete works in Javascript,
    because you can indirectly delete a property of the prototype using
    only the child. I don't think that should be possible. The prototype
    should not be mutable through a child reference, only through a direct
    reference.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: prototype-based vs class-based OOP langauges

      > I would like to know the benefits for javascripts' prototype objects as[color=blue]
      > compared to say java class-based objects.
      > The main points that I have on my mind are:
      >
      > 1. Is there a way to mark properties in prototype-based langauges as 'final
      > or 'private' as you can in java so that some properties are 'hidden' from
      > child objects.[/color]

      Yes. See Private Members in JavaScript,

      [color=blue]
      > 2. Is there a way for objects to limit the delete function in javascript, or
      > any prototype-based langauge, to prevent to object structure from been
      > 'broken'. ie if an object 'a' has a child object 'b', can object 'b' rely on
      > the fact object 'a' provides the features the next time object 'b' is used.[/color]

      No. However, you don't want to be using deep heirarchies as you did in
      static languages. You can get much farther going shallow with
      augmentation. See http://www.crockford.com/javascript/inheritance.html

      Comment

      Working...