HNHacker News
TopNewBestAskShowJobs

_getify

192 karma · joined April 23, 2013

Kyle Simpson is a web-oriented software engineer, widely acclaimed for his "You Don't Know JS" book series and nearly 1M hours viewed of his online courses. Kyle's superpower is asking better questions, who deeply believes in maximally using the minimally-necessary tools for any task. As a "human-centric technologist", he's passionate about bringing humans and technology together, evolving engineering organizations towards solving the right problems, in simpler ways. Kyle will always fight for the people behind the pixels.

[ my public key: https://keybase.io/getify; my proof: https://keybase.io/getify/sigs/SxqB6BQX2LQ92yiAfs1FYql_tPEhCqCGAZhnkap6tVY ]

submissionscomments
_getify··on JS Objects: Inherited a Mess
To expound on the metaphor I put in this article about inheritance and my mother-in-law:

> If you try to illustrate behavior delegation in terms of the "blueprint" metaphor, you quickly see how it totally breaks down. There's no way that my home, lacking a guest bedroom, could simply refer to another house, or to the original blueprints, to provide a bedroom for my mother-in-law when she comes to visit.

OTOH, I can delegate my mother-in-law to go stay in a hotel room. Or, I can delegate my mother-in-law to stay on a neighbor's couch, but I can send along sheets and pillows for her to use.

In either case, I'm not mutating my own home (inheritance) to handle her visit, I'm relying on someone else to handle it. But it's still my mother-in-law who I send elsewhere, and in fact, I can make sure she takes some "context" (aka, pillows) with her when she does.

Again, metaphors are never perfect, but I just thought I'd complete the perspective to contrast inheritance vs. behavior delegation.

_getify··on JS Objects: Inherited a Mess
The fact that Objective-C has a term "delegate" does not mean I can't argue that "behavior delegation" is an accurate term for any prototypical-inheritance mechanism, specifically JS's.

Objective-C taking a specific meaning for that term is nowhere near as ubiquitous as the point I made that "inheritance" has been so well defined across so many class-oriented languages (for decades!) that borrowing the term for [[Prototype]] is a confusing mistake.

_getify··on JS Objects: Inherited a Mess
I think you're going to see in part 3 (tomorrow) that it actually is quite simple and elegant to define "objects-only" delegation. You have to do away with constructors, "new", .prototype, and all that other stuff, which in part 2 (today) I'm going to argue are just distractions that keep us further from the simple power that is JS's [[Prototype]].
_getify··on JS Objects: Inherited a Mess
I think you'll see in part 3 (should be published tomorrow) why I'm arguing that "objects-only" is the MUCH simpler way to get [[Prototype]] styled code (that is, "behavior delegation"). However, it requires you to stop using constructors and "new" and .prototype and so forth. Part 2 (today) will argue why all those things are actually "distractions" from the simpler model.
_getify··on JS Objects: Inherited a Mess
I will not be directly addressing "parasitic inheritance" as I never saw it to be a terribly useful pattern, honestly. I see it very rarely used and even more rarely referenced by that name.

The point I'd make about it here, though, which is generically reflected in today's part 2 article, is that it's a fine pattern in and of itself, but the reason I think it's a "distraction" is that it actually moves away from the [[Prototype]] chain entirely.

You can use it as a bespoke pattern for combining two objects, but it's a mistake/confusion to think this has anything to do with JS's underlying delegation mechanism. The same point is made about mixins, and polymorphism (which also requires implicit mixins).

_getify··on JS Objects: Inherited a Mess
In part 2, which should be published today, I'll be addressing "class { }" and why I think it doesn't belong in JS. I think it's just more "distraction" that moves us away from a better clearer understanding of how [[Prototype]] behavior delegation works.
_getify··on JS Objects: Inherited a Mess
Actually, the evolution of the language has been adding "class-like" features over time. It was most definitely not designed originally to mimic classes. That's been the demand of all the classicists coming to the language. There are those of us who wish for the language to not have all those things.

In parts 2 and 3, I'll make the case for why I think all those things don't belong in JS and should be removed or avoided.

_getify··on JS Objects: Inherited a Mess
In part 2, which should be published later today, I am going to address "polymorphism" directly, and the punchline is that I'm going to argue that JS doesn't have a good "OO" way of doing relative super calls, and thus you should try to avoid polymorphism. The workaround is indeed an implicit mixin, where you do "ParentConstructor.prototype.method.call(this)", but even though that "works", it's brittle to be hard-wiring your "inheritance" chain across all your polymorphic overridden methods.
_getify··on JS Objects: Inherited a Mess
As the OP, I would argue that your metaphor actually does match well how delegation works not only in the real world, but also in JS.

For example, if I'm given a task by my boss, and he says "just make sure it gets done", I'll have a set of materials, say the specifications for a task, or an email thread discussing it, or something like that. When I decide to "delegate" it to another person on the team, I'm not going to expect that person to start from scratch... I'm going to hand them the "context" for them to tackle the task in. I'm going to say: "here's what you need to get started, go do it."

Metaphors are never perfect, by definition. But I take that perspective on your metaphor, and on JS's behavior delegation, and I think it fits decently.

← PreviousPage 4 of 4