{"id":387,"date":"2016-07-12T11:07:30","date_gmt":"2016-07-12T10:07:30","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=387"},"modified":"2022-07-25T08:35:45","modified_gmt":"2022-07-25T07:35:45","slug":"javascript-inheritance","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-inheritance\/","title":{"rendered":"Javascript inheritance"},"content":{"rendered":"<p>Source: http:\/\/stackoverflow.com\/a\/7510136<\/p>\n<pre lang=\"javascript\">\r\nfunction inherit(childClass,parentClass) {\r\n  var f=function(){}; \/\/ defining temp empty function\r\n  f.prototype=parentClass.prototype;\r\n  f.prototype.constructor=f;\r\n\r\n  childClass.prototype=new f;\r\n\r\n  childClass.prototype.constructor=childClass; \/\/ restoring proper constructor for child class\r\n  parentClass.prototype.constructor=parentClass; \/\/ restoring proper constructor for parent class\r\n}\r\n<\/pre>\n<p>Usage:<\/p>\n<pre lang=\"javascript\">\r\n\r\nEmployee = function Employee(\/*list of constructor parameters, if needed*\/) {\r\n}\r\nEmployee.prototype.paygrade = 1;\r\nEmployee.prototype.name = \"\";\r\nEmployee.prototype.dept = \"general\";\r\nEmployee.prototype.salary = function() {\r\n  return this.paygrade * 30000;\r\n}\r\n\r\n\r\nWorkerBee = function WorkerBee(\/*list of constructor parameters, if needed*\/) {\r\n  this.projects = [\"Project1\", \"Project2\"];\r\n}\r\ninherit(WorkerBee,Employee); \/\/ for this implementation of *inherit* must be placed just after defining constructor\r\nWorkerBee.prototype.paygrade = 2;\r\nWorkerBee.prototype.projects = null; \/\/ only literals and function-methods can properly initialized for instances with prototype\r\n\r\n\r\nEngineer = function Engineer(\/*list of constructor parameters, if needed*\/) {\r\n}\r\ninherit(Engineer,WorkerBee);\r\nEngineer.prototype.dept = \"Programming\";\r\nEngineer.prototype.language = \"Objective-C\";\r\n\r\n\r\n\r\n\r\nvar jane = new Engineer(\/*Engineer parameters if needed*\/);\r\nvar jill = new Engineer(\/*Engineer parameters if needed*\/);\r\nvar cow = new Employee(\/*Employee parameters if needed*\/);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source: http:\/\/stackoverflow.com\/a\/7510136 function inherit(childClass,parentClass) { var f=function(){}; \/\/ defining temp empty function f.prototype=parentClass.prototype; f.prototype.constructor=f; childClass.prototype=new f; childClass.prototype.constructor=childClass; \/\/ restoring proper constructor for child class parentClass.prototype.constructor=parentClass; \/\/ restoring proper constructor for parent class } Usage: Employee = function Employee(\/*list of constructor parameters, if needed*\/) { } Employee.prototype.paygrade = 1; Employee.prototype.name = &#8220;&#8221;; Employee.prototype.dept = &#8220;general&#8221;; Employee.prototype.salary [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/387\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}