News for hackers. Stuff that matters.
Re:Refactoring Legacy JavaScript Code to Use Classes by chickenfries on Tuesday March 07, 2017 @03:06PM (#13814214)
> By embracing prototypal inheritance you will write cleaner code with fewer errors.

[citation needed]

Classes are fine, they're just sugar over prototypal inheritance.

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.

  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.
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...
Re:Refactoring Legacy JavaScript Code to Use Classes by MrPatan on Wednesday March 08, 2017 @01:40PM (#13822482)
What I was trying to say is that without the class keyword, people used inheritance less, prototypal or whatever. Which is good, because inheritance sucks.