4 ms·
There's a whole depth of abstractions that people imply when they talk about OOP in JavaScript. In my mind they go something like this. 1) Using object lit
by zachrose 11y ago
There's a whole depth of abstractions that people imply when they talk about OOP in JavaScript. In my mind they go something like this.
1) Using object literals for values.
2) Using object literals with functions.
3) Using object literals with functions and calling them in contexts (`this`, `bind`, `apply`).
4) Using the `new` keyword.
5) Designing objects with inheritance hierarchies or prototype chains.
6) Designing mutable and stateful objects with indefinite life spans.
I don't understand the arguments that classical-ish inheritance in JS is bad while prototypical inheritance is somehow better. I've worked in codebases that liberally extend objects into different types of objects with long prototype chains, and IMHO they suffered from the same problems that people complain about in classical inheritance: tight coupling, premature/wrong abstractions, gradual violations of open/closed, etc.
Personally, I go through 1-4 liberally and 5 and 6 conservatively. IMHO, complaints about inheritance mechanisms seem to have something in common with the people who talk about the special needs of "large-scale applications". Why not just make smaller apps, or make smaller things that compose with less knowledge of where they are?
NB: It's entirely possible that I haven't run into the kind of complex requirements where liberal inheritance is a good fit. I'd be interested in hearing stories about when that worked out well.
- grayrest 11y ago> Why not just make smaller apps, or make smaller things that compose with less knowledge of where they are? The easy answer to this is that successful apps have a tendency to grow larger than expected and splitting them up requires care and lots of work. Class based inheritance provides a familiar way to split up the state space via encapsulation. It provides some boundaries instead of none and theoretically allows you to separate out your concerns and not have to think about the system-wide implications when writing code in your class. I'm in the functional camp and think classes aren't a great way to build software but I support the addition of `class` to ES6 since it shifts the ecosystem from a hundred slightly-incompatible versions to a single one. The mystery to me is why the people who advocate for JS classes are still writing JS rather than writing Typescript or Dart to further constrain their state space with a type system.
- deleted 11y ago[deleted]