{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/mixins.html","result":{"data":{"markdownRemark":{"id":"9356e1cd-77b2-56e1-b7f7-836aff8aca7c","excerpt":"Table of contents Introduction Mixin sample Understanding the sample Introduction ↥ back to top Along with traditional OO hierarchies, another popular way of…","html":"<h1 id=\"table-of-contents\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of contents</h1>\n<p><a href=\"#introduction\">Introduction</a></p>\n<p><a href=\"#mixin-sample\">Mixin sample</a></p>\n<p><a href=\"#understanding-the-sample\">Understanding the sample</a></p>\n<h1 id=\"introduction\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h1>\n<p><b><a href=\"#table-of-contents\">↥ back to top</a></b></p>\n<p>Along with traditional OO hierarchies, another popular way of building up classes from reusable components is to build them by combining simpler partial classes.\nYou may be familiar with the idea of mixins or traits for languages like Scala, and the pattern has also reached some popularity in the JavaScript community.</p>\n<h1 id=\"mixin-sample\"><a href=\"#mixin-sample\" aria-label=\"mixin sample permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Mixin sample</h1>\n<p><b><a href=\"#table-of-contents\">↥ back to top</a></b></p>\n<p>In the code below, we show how you can model mixins in TypeScript.\nAfter the code, we’ll break down how it works.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// Disposable Mixin</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Disposable {</span>\n<span style=\"color: #000000\">    isDisposed: boolean;</span>\n<span style=\"color: #000000\">    dispose() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isDisposed = </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Activatable Mixin</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Activatable {</span>\n<span style=\"color: #000000\">    isActive: boolean;</span>\n<span style=\"color: #000000\">    activate() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isActive = </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    deactivate() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isActive = </span><span style=\"color: #0000FF\">false</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> SmartObject {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">        setInterval(() </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> console.log(</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isActive + </span><span style=\"color: #A31515\">\" : \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isDisposed), </span><span style=\"color: #09835A\">500</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">    interact() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.activate();</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> SmartObject </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Disposable, Activatable {}</span>\n<span style=\"color: #000000\">applyMixins(SmartObject, [Disposable, Activatable]);</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> smartObj = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> SmartObject();</span>\n<span style=\"color: #000000\">setTimeout(() </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> smartObj.interact(), </span><span style=\"color: #09835A\">1000</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #008000\">////////////////////////////////////////</span>\n<span style=\"color: #008000\">// In your runtime library somewhere</span>\n<span style=\"color: #008000\">////////////////////////////////////////</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> applyMixins(derivedCtor: any, baseCtors: any[]) {</span>\n<span style=\"color: #000000\">    baseCtors.forEach(baseCtor </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">        Object.getOwnPropertyNames(baseCtor.prototype).forEach(name </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">            Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));</span>\n<span style=\"color: #000000\">        });</span>\n<span style=\"color: #000000\">    });</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"understanding-the-sample\"><a href=\"#understanding-the-sample\" aria-label=\"understanding the sample permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Understanding the sample</h1>\n<p><b><a href=\"#table-of-contents\">↥ back to top</a></b></p>\n<p>The code sample starts with the two classes that will act as our mixins.\nYou can see each one is focused on a particular activity or capability.\nWe’ll later mix these together to form a new class from both capabilities.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// Disposable Mixin</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Disposable {</span>\n<span style=\"color: #000000\">    isDisposed: boolean;</span>\n<span style=\"color: #000000\">    dispose() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isDisposed = </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Activatable Mixin</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Activatable {</span>\n<span style=\"color: #000000\">    isActive: boolean;</span>\n<span style=\"color: #000000\">    activate() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isActive = </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    deactivate() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.isActive = </span><span style=\"color: #0000FF\">false</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Next, we’ll create the class that will handle the combination of the two mixins.\nLet’s look at this in more detail to see how it does this:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> SmartObject {</span>\n<span style=\"color: #000000\">    ...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> SmartObject </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Disposable, Activatable {}</span></code></div></pre>\n<p>The first thing you may notice in the above is that instead of trying to extend <code>Disposable</code> and <code>Activatable</code> in <code>SmartObject</code> class, we extend them in <code>SmartObject</code> interface. <code>SmartObject</code> interface will be mixed into the <code>SmartObject</code> class due to the <a href=\"./Declaration%20Merging.md\">declaration merging</a>.</p>\n<p>This treats the classes as interfaces, and only mixes the types behind Disposable and Activatable into the SmartObject type rather than the implementation. This means that we’ll have to provide the implementation in class.\nExcept, that’s exactly what we want to avoid by using mixins.</p>\n<p>Finally, we mix our mixins into the class implementation.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">applyMixins(SmartObject, [Disposable, Activatable]);</span></code></div></pre>\n<p>Lastly, we create a helper function that will do the mixing for us.\nThis will run through the properties of each of the mixins and copy them over to the target of the mixins, filling out the stand-in properties with their implementations.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> applyMixins(derivedCtor: any, baseCtors: any[]) {</span>\n<span style=\"color: #000000\">    baseCtors.forEach(baseCtor </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">        Object.getOwnPropertyNames(baseCtor.prototype).forEach(name </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">            Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));</span>\n<span style=\"color: #000000\">        });</span>\n<span style=\"color: #000000\">    });</span>\n<span style=\"color: #000000\">}</span></code></div></pre>","headings":[{"value":"Table of contents","depth":1},{"value":"Introduction","depth":1},{"value":"Mixin sample","depth":1},{"value":"Understanding the sample","depth":1}],"frontmatter":{"permalink":"/docs/handbook/mixins.html","title":"Mixins"}}},"pageContext":{"slug":"/docs/handbook/mixins.html","isOldHandbook":true}}}