{"id":36,"date":"2024-12-30T15:20:50","date_gmt":"2024-12-30T15:20:50","guid":{"rendered":"https:\/\/programming.rexthestrange.com\/?p=36"},"modified":"2024-12-30T15:20:50","modified_gmt":"2024-12-30T15:20:50","slug":"multiple-inheritance","status":"publish","type":"post","link":"https:\/\/programming.rexthestrange.com\/?p=36","title":{"rendered":"Multiple Inheritance"},"content":{"rendered":"\n<p>Do you ever wish for multiple inheritance? That is, being able to inherit the methods and properties from multiple classes? For example, let&#8217;s say you have:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassA {\n\tpublic varA;\n\tpublic functionA {\n\t\treturn varA\n\t}\n}\n\nclass ClassB {\n\tpublic varB;\n\tpublic functionB {\n\t\treturn varB\n\t}\n}<\/code><\/pre>\n\n\n\n<p>And you want<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassC {\n\tpublic varA;\n\tpublic varB;\n\n\tpublic functionA {\n\t\treturn varA;\n\t}\n\n\tpublic functionB {\n\t\treturn varB\n\t}\n}<\/code><\/pre>\n\n\n\n<p>But you don&#8217;t want to have to spell it out because there&#8217;s a lot more in functions functionA and functionB than I&#8217;ve put here.<\/p>\n\n\n\n<p>Conventional wisdom says that interfaces are a way to get around the desire for multiple inheritance, but that&#8217;s not true because interfaces only provide the signature for the methods, not the implementation.<\/p>\n\n\n\n<p>Well, JavaScript is a stunningly versatile language. You can mimic multiple inheritance by creating a method to copy the functions from A and B to C. I&#8217;ve prototyped the method (and I thumb my nose at people who say this is bad practice &#8211; that&#8217;s how the language was designed). It looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Object.prototype.inherit = function (ancestor) {\n\tfor (let property in ancestor &#91;\"prototype\"]) {\n\t\tthis &#91;property] = ancestor &#91;\"prototype\"]&#91;property];\n\t}\/\/ for;\n}\/\/ inherit;\n<\/code><\/pre>\n\n\n\n<p>It can be invoked in your class constructor, thusly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassC {\n\tconstructor () {\n\t\tthis.inherit (ClassA);\n\t\tthis.inherit (ClassB);\n\t}\n}<\/code><\/pre>\n\n\n\n<p>It&#8217;s not exactly multiple inheritance, but it comes close.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you ever wish for multiple inheritance? That is, being able to inherit the methods and properties from multiple classes? For example, let&#8217;s say you have: And you want But you don&#8217;t want to have to spell it out because there&#8217;s a lot more in functions functionA and functionB than I&#8217;ve put here. Conventional wisdom [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":1,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}