5 ms·
> I already have a hard enough time teaching programmers about prototypical inheritance without the language actively lying to them. One way to look at it is t
by munificent 8y ago
> I already have a hard enough time teaching programmers about prototypical inheritance without the language actively lying to them.
One way to look at it is that you could consider not teaching the underlying prototypal inheritance model at all. If students will rarely run into code that relies on it, it may be vestigial semantics.
Think of it like how people used to teach patterns in C for doing object-oriented programming. For example, you could have a struct whose first field was a pointer to a table of functions for the operations you could perform on the object. In other words, how to implement your own vtables from scratch.
When C++ came along, it was still useful to understand vtables sometimes, but it wasn't something every programmer needed to fully internalize. C++ let them program at a higher level of abstraction.
> And it feels like an odd move in that class based OOP isn't the zeitgeist it once was.
Citation needed. :) Two of the newest, fastest growing languages are class-based: Kotlin and Swift. I'm not aware of any new prototype-based language that has noticeable growth.
Prototypes are a neat idea, but I think the market has pretty clearly shown that classes are a better practical approach to OOP.
- agd621 8y agoI don’t disagree that prototype is rare in real world practice but, anecdotally, it has come up in ~20% of my interviews in some fashion. That’s enough that I would be upset if the person teaching me JavaScript didn’t also teach me prototypal inheritance.
- thekaleb 8y agoI only bring up the prototype to determine if I would classify the interviewee as a senior JavaScript developer or not.
- adrianhel 8y agoObject.defineProperty('senior', { value: true, enumerable: false })
- scarface74 8y agoThere are two things I would expect even a mid level developer to know - how prototypical inheritance works and how “this” works in JS.
- bpicolo 8y agoIn modern times that's something you can easily throw far back in mindbank, because in practice using ES6 it almost never comes up, and it's super easy to do a quick refresher on the semantics. If I didn't know that interviewers tended to ask about it in frontend inteviews (because they totally do), would never bother to keep up to date with the semantics
- danShumway 8y agoI'm skeptical that classes make prototypal inheritance safe to ignore, because they're not a perfect abstraction. Opinion me, classes in Javascript do not act like classes in other langagues. In particular, I still run into usage of `call` and `apply` on a regular basis. class Foo { constructor () { this.foo = 0; } log () { return this.foo; } } (new Foo()).log.call({ foo: 2 }); I guess I can explain the above without prototype, but it's certainly weird behavior for a classical OOP language, and at the very least it's dancing around prototype-like behavior. Or, much worse: class Bar { constructor () { return new Foo(); } } And you can say, "how often is a student ever going to run into that case", but part of being a student is experimenting with code. If an abstraction only holds as long as you never do something weird, then it's probably not a very good abstraction. In that case it's worth asking, "is it good language design to have a feature give surprising results as soon as you do something that's even slightly off of the beaten path?"
- scarface74 8y agoWell, in that case the abstraction of private methods isn’t good either because there are ways to bypass them in C++ by passing a function pointer or in C# via reflection.
- danShumway 8y agoArguably, yes. However, I would say there's some difference in severity between a surprising behavior that results from the intersection between two features, and a surprising behavior that results because the way a feature is described is literally inaccurate. You can break Javascript classes without using any other feature of the language. You don't have to resort to something like Proxies, or overriding native prototypes. This is because it's not that other parts of Javascript provide escape hatches to get around behavior, it's that the class syntax itself does not cover the entirety of the prototype system it is trying to obscure. It's not ideal to have any breakage in an abstraction, but some breaks are worse than others. To me, edge cases like this: let foo = function () { this.x = 5; } class Bar extends foo { //No error? But why? constructor () { super(); } } foo = function () { this.x = 7; } console.log((new Bar()).x); // 5, not 7 or undefined. are on a whole nother level of leaky abstractions over even some of the worst parts of C#. To me, this is an edge case that nearly any reasonably imaginative student would likely find at some point using only very basic components of the Javascript language. And I have no idea how I would even begin to tackle explaining this without saying, "Okay, throw out everything I taught you about classes, here's how the language really works."
- zzo38computer 8y agoI do not use the "class" command in JavaScript (although I know it just does prototype inheritance), and think it was unnecessary to add (but you can't really remove it now). I think prototype inheritance is often more useful than class inheritance; I do not agree that classes are necessarily better. Prototype inheritance can be used for more kind of things (especially with Object.create and Object.getPrototypeOf; I sometimes use these).