0

I keep hearing that one of the reasons JavaScript is such a superior language is because it doesn't have classes. Instead, it has this magical thing called Prototypal Inheritance.

Correct me If I'm wrong but from what I understand, Object.prototype and Object.prototype.constructor (which runs some native code) are the two fundamental entities in Javascript. How is that different from a class?

What is the need for every function to have a .prototype if not to have a "blueprint" for object creation when called with the newoperator?

AyushISM
  • 381
  • 7
  • 21
  • 1
    Possible duplicate of [prototype based vs. class based inheritance](http://stackoverflow.com/questions/816071/prototype-based-vs-class-based-inheritance) – Mike Cluck May 13 '16 at 16:34
  • 1
    Nice question. What you describe is exactly how TypeScript takes ES6 classes and compiles them to ES5. They are our best bet for now. If you look at the output JS classes constructor objects become `object.prototype.something` – Ben Racicot May 13 '16 at 16:38
  • 1
    Where do you "keep hearing" this? These are bizarre claims. –  May 13 '16 at 16:40
  • ...in any case, a prototype is not a blueprint. It is an object used for looking up properties that don't exist on the original object. A prototype object can be, and often is, shared between many different objects. –  May 13 '16 at 16:45
  • @squint What part is bizarre? That I've heard people say this? Or that protoypal OO is better than classical OO? – AyushISM May 13 '16 at 16:48
  • 1
    That anyone would make a blanket statement that JS is a superior language because of its prototypal inheritance *(or because of any reason for that matter)*, and that there's anything magical about it. –  May 13 '16 at 16:49

1 Answers1

0

This question can stem into a very deep discussion if you dig enough but the simplest metaphor (an widely used) I can think of is to think of a "class" as a blueprint and an "object" as a house. In a class-based system, you can 'build' (instantiate) houses from a blueprint. And you can 'live' (call methods, etc) in these houses. But you can't 'live' in the blueprints. They serve solely to build houses from.

In JS, you don't have blueprints. You just have houses. You can copy the houses and 'live' in it and make each one different if you desire (save individual state, etc). But they are all copies of each other. There is no blueprint involved.

Eric N
  • 2,136
  • 13
  • 13
  • 3
    and if you add a pool to the parent house all children houses get a pool – maioman May 13 '16 at 16:59
  • 4
    @maioman: Only the parent house gets a pool. The children houses get to use their parents' pool. –  May 13 '16 at 17:03
  • @Eric You should probably explain that there's no actual copying done in prototypal inheritance. Your second paragraph seems to suggest that each object inheriting from another gets an actual copy of that inherited object. –  May 13 '16 at 17:05
  • @squint they get access to the same pool if you're adding a pool .. but you could have a build pool method (parent and child houses could make many pools) – maioman May 13 '16 at 17:10