{"id":1203,"date":"2022-08-30T15:14:06","date_gmt":"2022-08-30T15:14:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/calling-parents-constructor-in-javascript-oop-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:06","modified_gmt":"2022-08-30T15:14:06","slug":"calling-parents-constructor-in-javascript-oop-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/calling-parents-constructor-in-javascript-oop-collection-of-common-programming-errors\/","title":{"rendered":"Calling Parent&#39;s Constructor In JavaScript OOP-Collection of common programming errors"},"content":{"rendered":"<p>I have created two objects:<\/p>\n<p>Cat extends Mammal. Both objects have constructor which takes one parameter called config. I am trying to overwrite Mammals constructor in Cat&#8217;s constructor but I am getting strange results:<\/p>\n<pre><code>function Mammal(config) {\n    this.config = config;\n    console.log(this.config);\n}\n\nfunction Cat(config) {\n    \/\/ call parent constructor\n    Mammal.call(this, config);\n}\nCat.prototype = new Mammal();\n\nvar felix = new Cat({\n    \"name\": \"Felix\"\n});\n<\/code><\/pre>\n<p>This prints in the console:<\/p>\n<pre><code>undefined fiddle.jshell.net\/_display\/:23\nObject {name: \"Felix\"} \n<\/code><\/pre>\n<p>Why is the parent constructor called twice? And why, when it&#8217;s called the first time, this.config is undefined? I am assigning the property. Could you help me fix this code?<\/p>\n<p>http:\/\/jsfiddle.net\/DS7zA\/<\/p>\n<ol>\n<li>\n<p>It&#8217;s called twice because <em>you<\/em> call it with <code>Cat.prototype = new Mammal()<\/code>. You&#8217;re creating the prototype by copying from an <em>instance<\/em> of <code>Mammal<\/code>, rather than from the &#8220;prototypical&#8221; <code>Mammal<\/code>.<\/p>\n<p>The correct line would be:<\/p>\n<pre><code>Cat.prototype = Object.create(Mammal.prototype);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:09:25. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have created two objects: Cat extends Mammal. Both objects have constructor which takes one parameter called config. I am trying to overwrite Mammals constructor in Cat&#8217;s constructor but I am getting strange results: function Mammal(config) { this.config = config; console.log(this.config); } function Cat(config) { \/\/ call parent constructor Mammal.call(this, config); } Cat.prototype = new [&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-1203","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1203","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=1203"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1203\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}