views:

500

answers:

3

So I finally stopped dragging my feet all these years and decided to learn JavaScript "properly". One of the most head-scratching elements of the languages design is it's implementation of inheritance. Having experience in Ruby, I was really happy to see closures and dynamic typing; but for the life of me can't figure out what benefits are to be had from object instances using other instances for inheritance.

+1  A: 

Web Development: Prototypal Inheritance vs. Classical Inheritance

http://chamnapchhorn.blogspot.com/2009/05/prototypal-inheritance-vs-classical.html

Classical Vs prototypal inheritance - Stack Overflow

http://stackoverflow.com/questions/1450582/classical-vs-prototypal-inheritance

ratty
I think its better to summarise the contents of links rather than pasting the link (something I myself used to do), unless its another SO link. This is because links/sites go down and you loose the response to the question, and it potentially affects the search results.
James Westgate
@james thank you for ur comment
ratty
+6  A: 

Allow me to actually answer the question inline.

Prototype inheritance has the following virtues:

  1. It is better suited to dynamic languages because the inheritance is as dynamic as the environment it is in. (The applicability to JavaScript should be obvious here.) This permits you to do things quickly on the fly like customizing classes without huge amounts of infrastructure code.
  2. It is easier to implement a prototyping object scheme than the classic class/object dichotomy schemes.
  3. It eliminates the need for the complex sharp edges around the object model like "metaclasses" (I never metaclass I liked... sorry!) or "eigenvalues" or the like.

It has the following disadvantages however:

  1. Type checking a prototype language isn't impossible, but it's very, very difficult. Most "type checking" of prototypical languages is pure run-time "duck typing"-style checks. This is not suitable to all environments.
  2. It is similarly difficult to do things like optimizing method dispatch by static (or, often, even dynamic!) analysis. It can (I stress: can) be very inefficient very easily.
  3. Similarly object creation can be (and usually is) much slower in a prototyping language than it can be in a more conventional class/object dichotomy scheme.

I think you can read between the lines above and come up with the corresponding advantages and disadvantages of traditional class/object schemes. There are, of course, more in each area so I'll leave the rest up to other people answering.

JUST MY correct OPINION
+4  A: 

IMO the major benefit of prototypal inheritance is its simplicity.

The prototypal nature of the language can confuse people who are classically trained, but it turns out that actually this is a really simple and powerful concept, diferential inheritance.

You don't need to make classification, your code is smaller, less redundant, objects inherit from other, more general objects.

If you think prototypically you will soon notice that you don't need classes...

Prototypal inheritance will be much more popular in the near future, the ECMAScript 5th Edition specification introduced the Object.create method, which allows you to produce a new object instance that inherits from another one in a really simple way:

var obj = Object.create(baseInstance);

This new version of the standard is being implemented by all browser vendors, and I think we will start to see more pure prototypal inheritance...

CMS