Re:Refactoring Legacy JavaScript Code to Use Classes
by tantalor on Tuesday March 07, 2017 @04:29PM
(#13815095)
Yes, this why the class keyword is great. Also it fixes inheritance which is not easy to do with prototypes.
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.
[ Parent
| on HN ]
Re:Refactoring Legacy JavaScript Code to Use Classes
by MrPatan on Tuesday March 07, 2017 @06:04PM
(#13816040)
That'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!
Re:Refactoring Legacy JavaScript Code to Use Classes
by Lazare on Tuesday March 07, 2017 @06:50PM
(#13816438)
> 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.
Re:Refactoring Legacy JavaScript Code to Use Classes
by cmrdporcupine on Tuesday March 07, 2017 @08:12PM
(#13816933)
Well 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)
Re:Refactoring Legacy JavaScript Code to Use Classes
by tchaffee on Tuesday March 07, 2017 @10:20PM
(#13817588)
It'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-in...