title: JS Objects: Distractions
url: http://davidwalsh.name/javascript-objects-distractions
hash_url: ff61babc668fdb73cc214268acc1d0ea

<h3>JS Objects: TL;DR</h3>&#13;
<p>JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its "prototypal inheritance" system, mostly due to the fact that "inheritance" isn't how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that JS has "behavior delegation" (merely delegation links between objects) fits naturally with how JS syntax works, which creates more sensible code without the need of helpers.</p>&#13;
<p>When you set aside distractions like mixins, polymorphism, composition, classes, constructors, and instances, and only focus on the objects that link to each other, you gain a powerful tool in behavior delegation that is easier to write, reason about, explain, and code-maintain. Simpler is better. JS is "objects-only" (OO). Leave the classes to those other languages!</p>&#13;
&#13;
<h3>Due Thanks</h3>&#13;
<p>I'd like to thank the following amazing devs for their generous time in feedback/tech review of this article series: David Bruant, Hugh Wood, Mark Trostler, and Mark McDonnell. I am also honored that David Walsh wanted to publish these articles on his fantastic blog.</p>&#13;
&#13;
<p>In <a href="http://davidwalsh.name/javascript-objects">part 1</a> of this article series, I went into great detail (aka, wordiness) about the differences between what the traditional definition of "inheritance" means and how JS's <code>[[Prototype]]</code> mechanism works. We saw that JS operates oppositely to "inheritance", being better labeled as "behavior delegation". If you haven't read it and you have any twinges of doubt or confusion about that statement, I'd encourage you to go read part 1 first.</p>&#13;
&#13;
<p>Inheritance implies, to an extent, copying of behavioral definition down the chain, whereas behavior delegation implies delegating behavior up the chain. These aren't just word semantics, but an important distinction that, when examined, can de-mystify a lot of confusing stuff around JS objects.</p>&#13;
&#13;
<p>I'm by far not the first dev to realize this truth about JS. What differs here is in my reaction to that realization. The response usually is layering on other concepts to smoothe out the rough edges or unexpected consequences of how "prototypal inheritance" can surprise us, to try to make JS <em>feel</em> more like classical OO.</p>&#13;
&#13;
<p><strong>I think those attempts just distract us from the plain truth of how JS works.</strong></p>&#13;
&#13;
<p>I would rather identify the things which are merely distractions, and set them aside, and embrace only the true essence of how JS's <code>[[Prototype]]</code> works. Rather than trying to make JS more "inheritance friendly", I'd rather rip out everything that confuses me (and others) into thinking JS has "inheritance" at all.</p>&#13;
&#13;
<h2>Types</h2>&#13;
<p>It's often cited that in JavaScript, if you declare a function and add things to that function's prototype, then that alone makes a definition of a custom "type", which can be <em>instantiated</em>. If we were in a traditional OO language, that sort of thinking might be more appropriate, but here in JS land, it's just one of many distractions.</p>&#13;
&#13;
<p>You're not really creating a new type in any real sense of that word. It's not a new type that will be revealed by the<code>typeof</code> operator, and it's not going to affect the internal <code>[[Class]]</code> characteristic of a value (what would be reported by default via <code>Object#toString()</code>). It is true that you can do some self-reflection to check if an object is an "instance of" some function's construction (via the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> operator</a>). But importantly,<code>foo1 instanceof Foo</code> is just following the internal <code>[[Prototype]]</code> chain of your object <code>foo1</code> to see if at any level of that chain it happens to find the <code>.prototype</code> object attached to the <code>Foo</code> function.</p>&#13;
&#13;
<p>In other words, the reflection you're doing is not about checking if the value is a specified type at all, nor is it about the function constructor. It's <strong>only</strong> about asking if one object is in another object's <code>[[Prototype]]</code> chain. The name and <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.6">semantics of the <code>instanceof</code> operator</a> (referring to "instances" and "constructor functions") are layering on extra and unnecessary meaning, which only confuses you into thinking there's anything more than simple <code>[[Prototype]]</code> chain checking going on.</p>&#13;
&#13;
<p>Some developers frown on the usage of <code>instanceof</code>, and so an alternate form of determining the "type" of some object is called <a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>, which is basically inferring a value's "type" by inspecting the object for one or more charateristic features, like a specific method or property.</p>&#13;
&#13;
<p>Either way, these aren't really "types", they're just approximations of types, which is one part of what makes JS's object mechanism more complicated than other languages.</p>&#13;
&#13;
<a name="mixins" id="mixins"/>&#13;
<h2>Mixins</h2>&#13;
<p>Another distraction is trying to mimic the automatic "copying" of <a href="http://www.joezimjs.com/javascript/javascript-mixins-functional-inheritance/">inheritance</a> by <a href="http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/">using the "mixin"</a> pattern, which essentially manually iterates through all the methods/properties on an object and makes a "copy" (techically just a reference for functions and objects) onto the target object.</p>&#13;
&#13;
<p>I'm not saying that mixins are bad -- they're a very useful pattern. However, <strong>mixins have nothing to do with the<code>[[Prototype]]</code> chain</strong> or inheritance or delegation or any of that -- they rely entirely on implicit assignment of<code>this</code> by having an "owning object" at the call-time of a function/method. They are, in fact, completely circumventing the <code>[[Prototype]]</code> chain.</p>&#13;
&#13;
<p>Take any two independent objects, call them <code>A</code> and <code>B</code> (they don't have to be linked via <code>[[Prototype]]</code> at all), and you can still mixin <code>A</code>'s stuff into <code>B</code>. If that style of code works for your situation, use it! But just note that it has nothing to do with <code>[[Prototype]]</code> or inheritance. Trying to think of them as related is <strong>just a distraction</strong>.</p>&#13;
&#13;
<p>Another related distraction is when the inevitable desire to create "multiple inheritance" comes up, because JavaScript only allows an object to be <code>[[Prototype]]</code> linked to <strong>one</strong> other object at a time. When we read about the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Details_of_the_Object_Model#No_multiple_inheritance">lack of multiple inheritance in JavaScript</a>, several problems come up, and various "solutions" are often proposed, but we never actually solve them, we just do more fancy hand-waiving to distract us from the difficulties that JS poses at the syntax/semantic level.</p>&#13;
&#13;
<p>For example, you basically end up doing some form of "mixin" to get multiple different sets of properties/methods added into your object, but these techniques don't, without <a href="https://shanetomlinson.com/2012/chains-use-composition-to-fake-multiple-inheritance-in-javascript/">elaborate and inefficient work-arounds</a>, gracefully handle collision if two of your "ancestor" objects have the same property or method name. Only one version of the property/method is going to end up on your object, and that's usually going to be the last one you mixed-in. There's not really a clean way to have your object reference the different versions simultaneously.</p>&#13;
&#13;
<p>Some people choose another distraction to resolve these problems, by using the "composition" pattern. Basically, instead of wiring your object <code>C</code> to both <code>A</code> and <code>B</code>, you just maintain a separate instance of each of <code>A</code> and<code>B</code> inside your <code>C</code> object's properties/members. Again, this is not a bad pattern, it has plenty of goodness to it.</p>&#13;
&#13;
<p><a href="http://www.crockford.com/javascript/inheritance.html">Parasitic Inheritance</a> is another example of a pattern that works around this "problem" that <code>[[Prototype]]</code> doesn't work like classes by simply avoiding <code>[[Prototype]]</code> altogether. It's a fine pattern, but I think it's a confusing distraction because it makes you <strong>feel</strong> like you're doing OO when you're not.</p>&#13;
&#13;
&#13;
<p>Whatever technique you use here, you basically end up ignoring the <code>[[Prototype]]</code> chain, and doing things manually, which means you've moved away from JavaScript's "prototypal inheritance" mechanism altogether.&#13;
&#13;
</p><h2>Polymorphism</h2>&#13;
<p>One particular distraction that ends up creating some of the most awkward code patterns we deal with in JS is polymorphism, which is the practice of having the same method or property name at different levels of your "inheritance chain", and then using <code>super</code>-style relative references to access ancestor versions of the same.</p>&#13;
&#13;
<p>The problem is mechanical: JavaScript provides a <code>this</code> property, but importantly it is always rooted at the bottom of the <code>[[Prototype]]</code> chain, not whatever level of the chain the current function was found at. While it's true that <code>this.foobar()</code> might end up resolving (finding) <code>foobar()</code> at an ancestor level of the chain, inside that call, his <code>this</code> will still be the original rooted <code>this</code> object.</p>&#13;
&#13;
<p>Put more simply, <code>this</code> is not relative, but absolute to the beginning of the call stack. If JS had a <code>super</code> or a<code>currentThis</code> (as <a href="https://gist.github.com/getify/5253319">I proposed recently</a>), then those references would be relative to whatever the currently resolved link in the <code>[[Prototype]]</code> chain was, which would allow you to make a relative reference to a link "above". But, JS does not currently have any such mechanism. And <code>this</code> being absolute rooted makes it an ineffective (or <a href="https://gist.github.com/getify/5254459">inefficient at best</a>, thus impractical) solution to these relative references that polymorphism requires.</p>&#13;
&#13;
<p>Most of the OO helper libraries try to provide a way for you to make <code>super</code> calls, but all of them end up having to do inefficient things under the covers to make that kind of relative call work.</p>&#13;
&#13;
<h2>class { .. }</h2>&#13;
<p>Lastly, I think the <a href="http://infrequently.org/2012/04/class-warfare/">long and hotly debated</a> topic of <a href="http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes"><code>class { .. }</code> syntax</a> that is coming to the language in ES6 represents more of the <em>attempt to cover up what JS actually does with what people wished JS did</em>. These sorts of distractions are not likely to make understanding JS's actual mechanisms better. Some <a href="http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/">speculate that it will make JS more approachable from traditional OO devotees</a>, which may be true at first, but I suspect the ultimate result is that they'll quickly become frustrated about how it doesn't work as they'd expect.</p>&#13;
&#13;
<p>What's important to understand is that the new class syntax we're getting is not introducing radically new behavior or a more classical version of inheritance. It's wrapping up how JS <code>[[Prototype]]</code> delegation currently works, in a <strong>syntax and semantics which come pre-loaded with lots of <del>baggage</del> understanding and expectation, which run quite contradictory to what you'll really get with JS classes</strong>. If you currently do not understand, or don't like, JS object "inheritance", the <code>class {..}</code> syntax is pretty unlikely to satisfy you.</p>&#13;
&#13;
<p>Yes, the syntax takes away some of the boilerplate of explicitly adding items to a "constructor" function's<code>.prototype</code> object, and goodness knows we all will love not having to type the <code>function</code> keyword as many times. Hooray! If you already fully understand the awkward parts of JS "classes", and you can't wait for<code>class {..}</code> to sugar up the syntax, I'm sure you're happy, but I also think you're probably in the minority. It's made far too many compromises to even make it into the language to fully please a broad range of totally opposite opinions.</p>&#13;
&#13;
<p>The underlying <code>[[Prototype]]</code> system isn't changing, and almost none of the difficulties we just outlined are getting measurably any better. The only exception is the addition of the <code>super</code> keyword. That will be a welcome change I suppose.</p>&#13;
&#13;
<p>Although, as a side note, the engine won't actually bind <code>super</code> dynamically (at call time) to the appropriate link in the <code>[[Prototype]]</code> chain, but will instead bind it statically (at definition time) based on the owning object of a function reference. This is going to possibly create some weird WTF's because the engine is going to have to create new function references on the fly as functions that use <code>super</code> are assigned around to different owning objects. It's possible (unconfirmed suspicion) that it may not work in all cases as you'd expect if <code>super</code> were instead bound dynamically.</p>&#13;
&#13;
<h2>Simplificationizing™</h2>&#13;
<p>We've just examined a variety of ways that many JS devs try to layer on extra abstractions and concepts on top of JS's core object mechanism. I argue that this is a mistake that takes us further from the beauty of core JavaScript. Rather than adding complexity to smoothe out the rough edges, I think we need to strip things out to get to the good stuff.</p>&#13;
&#13;
<p>In <a href="http://davidwalsh.name/javascript-objects-deconstruction">part 3</a>, I will address exactly that, taking JS from the more complex world of classes and inheritance back to the simpler world of objects and delegation links.</p>
