prototype-based vs class-based OOP langauges

N

Neil Morris

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
 
L

Lasse Reichstein Nielsen

Neil Morris said:
I would like to know the benefits for javascripts' prototype objects as
compared to say java class-based objects.

The advantage is the ease of dynamic modification, something that
isn't possible when your class structure is fixed at compile time.
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.

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

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
 
D

Douglas Crockford

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.

Yes. See Private Members in JavaScript,
http://www.crockford.com/javascript/private.html
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.

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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top