|
|
|
|
|
by jashkenas
5494 days ago
|
|
I would love to see an example backing your thesis, if you don't mind digging one up. I've had this debate with folks many times before, and literally every time it comes up, they're unable to find an example of using prototypal inheritance in a style that wouldn't also be considered classical. If you think that writing: var point = Object.create(Point);
... is somehow magically prototypal, while writing: var point = new Point;
... is unnaturally classical, I'll argue that you're missing the, ahem, point.They're two different ways of writing the exact same pattern. And, if we're being honest with ourselves, the "Point" object in that code should rightly be called a "class": it's the abstract object that defines the shape of all points. Of course, in JavaScript, we also tend to call it the "constructor function" (which it is, technically), and sometimes, the "prototype" (because it serves in that role). |
|
Prototypal inheritance makes a difference if one instance inherits field values (other than functions) from another instance. But this does not seem to be very common.
I believe one of the original use cases for prototypal inheritance were to save memory by letting GUI controls share common properties. Eg. if you had 10 buttons on the screen which all had the same color, same dimensions etc. With class based inheritance each instance would have their own copy of all properties, even if they have the same values in each instance.
I don't think that kind of optimization is relevant in modern JavaScript.