{"id":221766,"date":"2020-01-28T04:56:35","date_gmt":"2020-01-28T04:56:35","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=221766"},"modified":"2020-01-28T05:24:45","modified_gmt":"2020-01-28T05:24:45","slug":"inheritance-in-javascript","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/","title":{"rendered":"Inheritance in Javascript"},"content":{"rendered":"\n<p>Inheritance is an important concept in object-oriented programming. In the classical inheritance, methods from parent class get copied into child class.<\/p>\n\n\n\n<p>      In JavaScript, inheritance is supported by using the prototype object. Javascript inheritance also is known as &#8220;Prototypal Inheritance&#8221; and &#8220;Behaviour Delegation&#8221;. &nbsp;JavaScript is not a class-based language although&nbsp;<em>class<\/em>&nbsp;keyword is introduced in ES2015, it is just a <strong>syntactical layer<\/strong>. JavaScript still works on&nbsp;the <em>prototype<\/em>&nbsp;chain<\/p>\n\n\n\n<p>       In JavaScript, objects have a special hidden property&nbsp;<code>[[Prototype]]<\/code>&nbsp;(as named in the specification), that is either&nbsp;<code>null<\/code>&nbsp;or references another object. That object is called \u201ca prototype<\/p>\n\n\n\n<p>First of all, we can create ClassTemp easily. Because there are no explicit classes, we can define a set of behavior (Temp class so\u2026) by just creating a function like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">var ClassTemp = function() {\n    this.classname = &quot;class Temp&quot;;\n}<\/pre>\n\n\n\n<p>This \u201cclass\u201d can be instantiated using the&nbsp;<code>new<\/code>&nbsp;keyword:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\nvar a = new ClassTemp();\nClassTemp.prototype.print = function() {\n    console.log(this.classname);\n}<\/pre>\n\n\n\n<p>And to use it using our object:<\/p>\n\n\n\n<p><code>a.print();<\/code><\/p>\n\n\n\n<p>So now we can put all the code in the single program.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">var ClassTemp = function() {\n    this.classname = &quot;class Temp&quot;;\n}\n\nClassTemp.prototype.print = function() {\n    console.log(this.classname);\n}\n\nvar a = new ClassTemp();\n\na.print();<\/pre>\n\n\n\n<p>Output Will be: Class A<\/p>\n\n\n\n<p><strong>Why we will use the inheritance in the javascript<\/strong>?<\/p>\n\n\n\n<p>There are multiple reasons to use the inheritance in the javascript which are following.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It&#8217;s simple.<\/li><li>It&#8217;s powerful.<\/li><li>It leads to smaller, less redundant code.<\/li><li>It&#8217;s dynamic and hence it&#8217;s better for dynamic languages.<\/li><\/ul>\n\n\n\n<p><strong>Only modify your&nbsp;own&nbsp;prototypes. Never modify the prototypes of standard JavaScript objects.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is an important concept in object-oriented programming. In the classical inheritance, methods from parent class get copied into child class. In JavaScript, inheritance is supported by using the prototype object. Javascript inheritance also is known as &#8220;Prototypal Inheritance&#8221; and &#8220;Behaviour Delegation&#8221;. &nbsp;JavaScript is not a class-based language although&nbsp;class&nbsp;keyword is introduced in ES2015, it is <a href=\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":226,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-221766","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Inheritance in Javascript - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inheritance in Javascript - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Inheritance is an important concept in object-oriented programming. In the classical inheritance, methods from parent class get copied into child class. In JavaScript, inheritance is supported by using the prototype object. Javascript inheritance also is known as &#8220;Prototypal Inheritance&#8221; and &#8220;Behaviour Delegation&#8221;. &nbsp;JavaScript is not a class-based language although&nbsp;class&nbsp;keyword is introduced in ES2015, it is [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-28T04:56:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-28T05:24:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Arun Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Arun Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\"},\"author\":{\"name\":\"Arun Kumar\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/880586fc1e4703d9e951d86655682ffd\"},\"headline\":\"Inheritance in Javascript\",\"datePublished\":\"2020-01-28T04:56:35+00:00\",\"dateModified\":\"2020-01-28T05:24:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\"},\"wordCount\":229,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\",\"url\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\",\"name\":\"Inheritance in Javascript - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2020-01-28T04:56:35+00:00\",\"dateModified\":\"2020-01-28T05:24:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inheritance in Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/880586fc1e4703d9e951d86655682ffd\",\"name\":\"Arun Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/22744c127478ae0d9d9efb702c1379e4ae8c5538423ba70bb9a37dfeb92861c9?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/22744c127478ae0d9d9efb702c1379e4ae8c5538423ba70bb9a37dfeb92861c9?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Arun Kumar\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/arun-kumar350\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inheritance in Javascript - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Inheritance in Javascript - Webkul Blog","og_description":"Inheritance is an important concept in object-oriented programming. In the classical inheritance, methods from parent class get copied into child class. In JavaScript, inheritance is supported by using the prototype object. Javascript inheritance also is known as &#8220;Prototypal Inheritance&#8221; and &#8220;Behaviour Delegation&#8221;. &nbsp;JavaScript is not a class-based language although&nbsp;class&nbsp;keyword is introduced in ES2015, it is [...]","og_url":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2020-01-28T04:56:35+00:00","article_modified_time":"2020-01-28T05:24:45+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Arun Kumar","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Arun Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/"},"author":{"name":"Arun Kumar","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/880586fc1e4703d9e951d86655682ffd"},"headline":"Inheritance in Javascript","datePublished":"2020-01-28T04:56:35+00:00","dateModified":"2020-01-28T05:24:45+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/"},"wordCount":229,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/","url":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/","name":"Inheritance in Javascript - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2020-01-28T04:56:35+00:00","dateModified":"2020-01-28T05:24:45+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/inheritance-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/inheritance-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Inheritance in Javascript"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/880586fc1e4703d9e951d86655682ffd","name":"Arun Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/22744c127478ae0d9d9efb702c1379e4ae8c5538423ba70bb9a37dfeb92861c9?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/22744c127478ae0d9d9efb702c1379e4ae8c5538423ba70bb9a37dfeb92861c9?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Arun Kumar"},"url":"https:\/\/webkul.com\/blog\/author\/arun-kumar350\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/221766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/226"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=221766"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/221766\/revisions"}],"predecessor-version":[{"id":226234,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/221766\/revisions\/226234"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=221766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=221766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=221766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}