3 Different Kinds of Prototypal Inheritance: ES6+ Edition
Eric Elliott
91816

Why classes?

First, we’ll use a closure to create data privacy:

const A = (function() {
const a = new WeakMap()
 return (Heritage = Object) => class extends Heritage {
constructor() {
super()
a.set(this, 'a')
}
  getA() {
return a.get(this)
}
}
}())

Here’s another:

const B = (function() {
const a = new WeakMap()
 return (Heritage = Object) => class extends Heritage {
constructor() {
super()
a.set(this, 'b')
}
 getB() {
return a.get(this)
}
}
}())

Those `a`’s are not typos. The point is to demonstrate that `a` and `b`’s private variables won’t clash.

But here’s the real treat:

class C extends A(B()) {}
const foo = new C()
console.log(foo.getA())
console.log(foo.getB())

WAT? Yeah. You just inherited privileged methods and private data from two sources at the same time.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.