To be honest I don't know what was the motivation of TC39 for adding class syntax to ECMAScript. Perhaps for easier transition of object oriented people (Java/C++) into JavaScript? Any way, as you mentioned, delegation pattern
(I prefer this name over prototypical inheritance
) gives more flexibility. And I think we should use it, always.
Firstly, it has better performance against object-oriented approach. If you do not want to copy common set of properties to large number of objects, you just use Object.create(commonProperties)
to create new object based on the object containing common set of properties. Within class approach it's not possible, you must create set of common properties for each object instance, which might have greater memory consumption impact.
What's more, class inheritance by design leads to tight coupling. Imagine you want to model tesla
car in your application. Probably first thought is to define ElectricCar
which extends Car
class. Then we need some gas-based car, let's say ford
. So you define GasCar
which also extends Car
class. But what happen, when we want another car, let's say steem
, which have both electric car and gas-based car capabilities? Which class should we instantiate, both of them? Object composition is more flexible: we might just call const steem = Object.extend({}, electricCarCapabilities, gasCarCapabilities)
where objects have functions and properties describing specific set of capabilities.
JavaScript is so powerful tool, it does not need object-oriented paradigm to be part of the specification. I think functional programming
is more fun. Strongly encourage you to make some shift in way of thinking and check it yourself. I did so.
Nice article by the way!
RE: Prototype Properties in the New Class Syntax in JavaScript
You are viewing a single comment's thread from:
Prototype Properties in the New Class Syntax in JavaScript
Yeah this was it. People coming from more traditional OOP languages were confused by prototypes and the fact that there seemed to be different ways to do OO in JS, so they added some syntactic sugar to make it easier for people moving to JS. They saw familiar looking class structures and it made them feel more comfortable.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Wow, your comment is much more informative that my post. Thank you!
Multiple inheritance is really necessary for a decent object-oriented system, but it is left to mixins and is not part of the class syntax. I just want to know the reason data properties were left out. Another oddity is that the methods in class syntax are not separated by commas as is the usual syntax in object literals.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks! According to reason of data properties were left out. I think it's more by design. Class is a blueprint, general shape of object and only instances are supplied with data. It's common for object-oriented programming like Java/C#. Defining prototype properties for classes might be tricky, as it's a try of mixing two extremely different approaches.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Yes, I guess that's right. Anyway, both syntax options are available and can be used according to the needs.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit