{"id":1003,"date":"2022-08-30T15:10:46","date_gmt":"2022-08-30T15:10:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-specialize-a-javascript-class-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:46","modified_gmt":"2022-08-30T15:10:46","slug":"how-to-specialize-a-javascript-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-specialize-a-javascript-class-collection-of-common-programming-errors\/","title":{"rendered":"How to specialize a Javascript class?-Collection of common programming errors"},"content":{"rendered":"<p>I was playing with JavaScript class inheritance (I am using node.js). I get &#8220;undefined&#8221; values for instances of the child class. Here my example:<\/p>\n<p>I define a Squirrel class and I want to specialize this class in a KillerSquirrel child class. I want create instances of both the Squirrel and the KillerSquirrel classes.<\/p>\n<pre><code>function Squirrel(name, color) {\n        this.name = name;\n        this.color = color;\n        console.log(\"A new \" + this.color + \" squirrel named \" + this.name + \" is born!!!\");\n    };\n\n    \/\/ add a new method called speak()\n    Squirrel.prototype.speak = function(text) {\n        console.log(this.name + \" squirrel says: '\" + text + \"'\");\n    };\n\n    \/\/ specialize the Squirrel class by creating a new KillerSquirrel constructor \n    function KillerSquirrel(name, color) {    \n      this.soul = 'very bad';\n      console.log(\"Bhrrr ... a new \" + this.color + \" killer squirrel named \" + this.name + \" is born!!!\");\n    }  \n    KillerSquirrel.prototype.__proto__ = Squirrel.prototype;\n\n    \/\/ add kill method  \n    KillerSquirrel.prototype.kill = function(obj){  \n      console.log(this.name + \" squirrel killed \" + obj.name + \" squirrel\"); \n    }\n\n    \/\/ replace the speak() method  \n    KillerSquirrel.prototype.speak = function(text) {\n                       console.log(this.name + \" squirrel says: 'Grhhh! \" + text + \"' Grhhh!\");\n    };\n\n    var squirrel = new Squirrel(\"Gummy\", \"brown\");\n    squirrel.speak(\"My name is \" + squirrel.name);\n    var killer = new KillerSquirrel(\"Mr.Hide\", \"black\");\n    killer.speak(\"My name is \" + killer.name);\n<\/code><\/pre>\n<p>I create a squirrel instance of Squirrel using its constructor and passing some values to the constructor and this works as expected. When I try to create an instance of child class KillerSquirrel using its constructor and passing some values to it, the killer squirrel instance has &#8220;undefined properties&#8221;.<\/p>\n<p>see:<\/p>\n<pre><code>$ node killersquirrel.js\nA new brown squirrel named Gummy is born!!!\nGummy squirrel says: 'My name is Gummy'\nBhrrr ... a new undefined killer squirrel named undefined is born!!!\nundefined squirrel says: 'Grhhh! My name is undefined' Grhhh!\n<\/code><\/pre>\n<ol>\n<li>\n<p>Subclass constructor should call superclass constructor manually by special construct (apply or call), like this:<\/p>\n<pre><code>function KillerSquirrel(name, color) {\n  Squirrel.apply(this, arguments);    \n  this.soul = 'very bad';\n  console.log(\"Bhrrr ... a new \" + this.color + \" killer squirrel named \" + this.name + \" is born!!!\");\n}  \n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>function KillerSquirrel(name, color) {\n  Squirrel.call(this, name, color);    \n  this.soul = 'very bad';\n  console.log(\"Bhrrr ... a new \" + this.color + \" killer squirrel named \" + this.name + \" is born!!!\");\n}  \n<\/code><\/pre>\n<p>though for this case (when the arguments are the same) the first form is preferred.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:12:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I was playing with JavaScript class inheritance (I am using node.js). I get &#8220;undefined&#8221; values for instances of the child class. Here my example: I define a Squirrel class and I want to specialize this class in a KillerSquirrel child class. I want create instances of both the Squirrel and the KillerSquirrel classes. function Squirrel(name, [&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-1003","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1003","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=1003"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1003\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}