{"id":886,"date":"2022-08-30T15:08:49","date_gmt":"2022-08-30T15:08:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-prototypal-inheritance-doubt-ii-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:49","modified_gmt":"2022-08-30T15:08:49","slug":"javascript-prototypal-inheritance-doubt-ii-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-prototypal-inheritance-doubt-ii-collection-of-common-programming-errors\/","title":{"rendered":"Javascript Prototypal Inheritance Doubt II-Collection of common programming errors"},"content":{"rendered":"<p>I know it&#8217;s already been answered but, there&#8217;s a better way to do inheritance. Calling a constructor just for the purpose of inheritance is not desirable. One of the undesired effects is.<\/p>\n<pre><code>function Base() {this.a = \"A\"}\nfunction Child() {this.b = \"B\"};\n\nChild.prototype = new Base();\n<\/code><\/pre>\n<p>Now You&#8217;ve added property &#8220;a&#8221; to the prototype of Child that you didn&#8217;t intend to.<\/p>\n<p>Here&#8217;s the right way (I didn&#8217;t invent this, Ext-JS and other libs use this)<\/p>\n<pre><code>\/\/ This is used to avoid calling a base class's constructor just to setup inheritance.\nfunction SurrogateCtor() {}\n\n\/**\n * Sets a contructor to inherit from another constructor\n *\/\nfunction extend(BaseCtor, DerivedCtor) {\n  \/\/ Copy the prototype to the surrogate constructor\n  SurrogateCtor.prototype = BaseCtor.prototype;\n  \/\/ this sets up the inheritance chain\n  DerivedCtor.prototype = new SurrogateCtor();\n  \/\/ Fix the constructor property, otherwise it would point to the BaseCtor\n  DerivedCtor.prototype.constructor = DerivedCtor;\n  \/\/ Might as well add a property to the constructor to \n  \/\/ allow for simpler calling of base class's method\n  DerivedCtor.superclass = BaseCtor;\n}\n\nfunction Base() {\n  this.a = \"A\";\n}\n\nBase.prototype.getA = function() {return this.a}\n\nfunction Derived() {\n  Derived.superclass.call(this);  \/\/ No need to reference the base class by name\n  this.b = \"B\";\n}\n\nextend(Base, Derived);\n\/\/ Have to set methods on the prototype after the call to extend\n\/\/ otherwise the prototype is overridden;\nDerived.prototype.getB = function(){return this.b};\nvar obj = new Derived();\n<\/code><\/pre>\n<p>An even easier way is to add a third parameter to extend where you specify the method of the derived class so that you don&#8217;t have to call extend and then add methods to the prototype<\/p>\n<pre><code>extend(BaseCtor, DerivedCtor, {\n  getB: function() {return this.b}\n});\n<\/code><\/pre>\n<p>Then there are many other things you could do for syntactic sugar.<\/p>\n<p>Blogged about it: http:\/\/js-bits.blogspot.com\/2010\/08\/javascript-inheritance-done-right.html<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:53:28. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I know it&#8217;s already been answered but, there&#8217;s a better way to do inheritance. Calling a constructor just for the purpose of inheritance is not desirable. One of the undesired effects is. function Base() {this.a = &#8220;A&#8221;} function Child() {this.b = &#8220;B&#8221;}; Child.prototype = new Base(); Now You&#8217;ve added property &#8220;a&#8221; to the prototype of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=886"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/886\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}