{"id":1007,"date":"2022-08-30T15:10:50","date_gmt":"2022-08-30T15:10:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-inheritance-without-child-prototype-new-parent-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:50","modified_gmt":"2022-08-30T15:10:50","slug":"javascript-inheritance-without-child-prototype-new-parent-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-inheritance-without-child-prototype-new-parent-collection-of-common-programming-errors\/","title":{"rendered":"Javascript inheritance without child.prototype = new parent();-Collection of common programming errors"},"content":{"rendered":"<p>I was playing around with javascript prototype chain inheritance and i came accross this funny behaviour.<\/p>\n<p>I have a parent class and a child class<\/p>\n<pre><code>\/\/class parent\nfunction parent(param_1) {\n    this.param_1 = param_1;\n\n    this.getObjWithParam = function(val) {\n        console.log(this);\n        console.log(\"Constructor value in parent class \" + this.param_1);\n        console.log(\"tguha ----&gt; parent,  val \" + val);\n    };\n};\n\n\/\/class child\nfunction child(param_1) {\n    parent.call(this, [ param_1 ]);\n};\n\nvar childObj = new child(100);\nchildObj.getObjWithParam(200);\n<\/code><\/pre>\n<p>and i get the output as<\/p>\n<pre><code>**&gt;child**\nConstructor value in parent class 100\ntguha ----&gt; parent,  val 200\n<\/code><\/pre>\n<p>and nowhere i&#8217;m doing \/\/child.prototype = new parent(); and still the parent class is inherited.<\/p>\n<p>Could anyone help me by explaining this scenario please.<\/p>\n<ol>\n<li>\n<p>The word prototype does not appear in this code. So nothing is being inherited. You create a new <code>child<\/code> and then explicitly run the <code>parent<\/code> constructor function on that new <code>child<\/code>. The <code>parent<\/code> constructor function then add a method to to the new <code>child<\/code>.<\/p>\n<p>If you put <code>getObjWithParam<\/code> on <code>parent.prototype.getObjWithParam<\/code> instead then you will see that it will not carry over.<\/p>\n<\/li>\n<li>\n<p>You invoke parent constructor in child constructor. Because parent constructor sets <code>this.getObjWithParam = function[...]<\/code> it&#8217;ll also set it for child. Notice that this has nothing to do with prototype. By invoking <code>parent.call(a, [...])<\/code> you invoke parent function and set scope to <code>a<\/code>. That means any modification made to <code>this<\/code> is also made to <code>a<\/code> (because it&#8217;s the same object).<\/p>\n<\/li>\n<li>\n<pre><code>parent.call(this, [ param_1 ]) \n<\/code><\/pre>\n<p>Assigns Parent.param_1 and Parent.getObjWithParam to childObj. This has nothing to do with inheritance. Consider this formulation:<\/p>\n<pre><code>var foo = {};\n\n\/\/class parent\nfunction parent(param_1) {\n    this.param_1 = param_1;\n\n    this.getObjWithParam = function(val) {\n        console.log(this.param_1);\n        console.log(val);\n    };\n};\n\n\/\/class child\nfunction child(param_1) {\n    parent.call(foo, [ param_1 ]);\n};\n\nvar childObj = new child( 'lets give foo a param_1');\n\nconsole.log( typeof childObj.getObjWithParam );\n\/\/ Undefined\n\nfoo.getObjWithParam();\n\/\/ [\"lets give foo a param_1\"]\n<\/code><\/pre>\n<p>here you are passing foo as a scope to .call() thus the properties are assigned to foo.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:12:37. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I was playing around with javascript prototype chain inheritance and i came accross this funny behaviour. I have a parent class and a child class \/\/class parent function parent(param_1) { this.param_1 = param_1; this.getObjWithParam = function(val) { console.log(this); console.log(&#8220;Constructor value in parent class &#8221; + this.param_1); console.log(&#8220;tguha &#8212;-&gt; parent, val &#8221; + val); }; }; [&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-1007","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1007","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=1007"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1007\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}