Common Misconceptions About Inheritance in JavaScript
Eric Elliott
1K66

Interesting article. Also, you have awesome hair.

One interesting thing I noticed when reading this was: With constructors, you can’t use ES5 getter/setters with them, you can only use them with object literals. For example, in your last example with a private var, it could also easily be written like so:

var secret = 'secret agent';
...
get profession () {
return secret;
}
...

Doing that with constructors is impossible. Doing that with ES6 classes is possible, but as far as I know, there’s no concept of a private var within the context of a class in ES6. So it’s also not possible.

Another thing you can’t do easily with constructors/classes is use .call() or .apply() on them. (It’s possible, just not a pretty solution). Not an issue with this pattern.

It’s as if there’s a lot of powerful features and methods on Object that work really well with this style of object instantiation but don’t work with the constructor or class pattern. Cool stuff.

Show your support

Clapping shows how much you appreciated Cameron Nokes’s story.