3 ms·
> By embracing prototypal inheritance you will write cleaner code with fewer errors. [citation needed] Classes are fine, they're just sugar over prototypal in
by chickenfries 10y ago
> By embracing prototypal inheritance you will write cleaner code with fewer errors.
[citation needed]
Classes are fine, they're just sugar over prototypal inheritance.
- tantalor 10y agoYes, this why the class keyword is great. Also it fixes inheritance which is not easy to do with prototypes. class A {} > class A {} class B extends A {} > class B extends A {} new B instanceof B > true new B instanceof A > true There is just 1 way to do this with classes, it is very concise, and it is impossible to do wrong, e.g., by forgetting to call the super class constructor. Try doing that with prototypes.
- MrPatan 10y agoThat's the thing, with prototypes you don't use inheritance, which is the good option. Now it's easy to do the wrong thing. Thanks ECMA!
- Lazare 10y ago> with prototypes you don't use inheritance That is 100% wrong. Prototypes are inheritance. Quoting from wikipedia: "Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of reusing existing objects via delegation that serve as prototypes." I have no idea what you were trying to say, but what you wrote is nonsense.
- cmrdporcupine 10y agoWell arguably the definition of inheritance there doesn't meet the poster's idea of inheritance. Prototype 'inheritance' is a kind of delegation and some prototype OO languages make that explicit by using different terminology (Self says 'delegate' and not 'parent' for example). But then arguably in class-based OO languages, classes don't "inherit" either and really just delegate to other classes. (FWIW I wrote a big chunk of the prototype-OO article on WP at one point. Not sure how much of that text still exists)
- tchaffee 10y agoIt's sort of wrong, but not really if we are using the commonly used definition of inheritance where we think of the strict hierarchical inheritance provided by classes. And you mention prototype delegation which is only one type of inheritance provided by javascript and is also hierarchical. There are two other types of inheritance you can use with javascript: functional inheritance, and concatenative inheritance. Concatenative inheritance is so different from class inheritance that most people don't even call it inheritance. They call it object composition. Which has been preferred over class inheritance as far back as the GoF "Favor object composition over class inheritance." More here: https://medium.com/javascript-scene/master-the-javascript-interview-what-s-the-difference-between-class-prototypal-inheritance-e4cd0a7562e9#.yb1ex5336 https://medium.com/javascript-scene/master-the-javascript-in...
- MrPatan 10y agoWhat I was trying to say is that without the class keyword, people used inheritance less, prototypal or whatever. Which is good, because inheritance sucks.