Re:JavaScript: prototype vs. class
by geoelectric on Wednesday April 04, 2018 @07:41PM
(#16760399)
To be absolutely fair, the pattern at the top is buggy in that you're supposed to also set
Foo.prototype.constructor = Foo;
...after replacing the prototype object that way. MDN (for example) covers this in a few pages, including:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Ob...
That said, it's a pretty common bug, particularly with stuff like:
Foo.prototype = Object.create(FooParent.prototype) // Crockford-style inheritance
console.log(Foo.prototype.constructor.name) // "FooParent"
Calling `super` is very anti-pattern in JS. Unlike classic OOP, parents are not static. Lots of very common things can rip that rug out from under you by modifying the parent in many different ways (createProperty, Symbols, proxies/reflection, etc).
Static methods do indeed exist without classes.
If you use `Object.create()` and ES6 object literal syntax you don't have to type `function` either. You get the added bonus of being easily able to wrap it in a normal function that avoids constructor weirdness