{"id":132,"date":"2015-07-22T11:38:00","date_gmt":"2015-07-22T11:38:00","guid":{"rendered":"https:\/\/puresourcecode.com\/index.php\/2015\/07\/22\/classical-inheritance-in-javascript\/"},"modified":"2015-07-22T11:38:00","modified_gmt":"2015-07-22T11:38:00","slug":"classical-inheritance-in-javascript","status":"publish","type":"post","link":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/","title":{"rendered":"Classical Inheritance in JavaScript"},"content":{"rendered":"<p>JavaScript is a <i>class-free<\/i>, object-oriented language, and as such,<br \/>\nit uses prototypal inheritance instead of classical inheritance. This can be<br \/>\npuzzling to programmers trained  in  conventional object-oriented languages<br \/>\nlike C++ and Java. JavaScript&#8217;s prototypal inheritance  has more expressive<br \/>\npower than classical inheritance, as we will see presently.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Java<\/th>\n<th>JavaScript<\/th>\n<\/tr>\n<tr>\n<td>Strongly-typed<\/td>\n<td>Loosely-typed<\/td>\n<\/tr>\n<tr>\n<td>Static<\/td>\n<td>Dynamic<\/td>\n<\/tr>\n<tr>\n<td>Classical<\/td>\n<td>Prototypal<\/td>\n<\/tr>\n<tr>\n<td>Classes<\/td>\n<td>Functions<\/td>\n<\/tr>\n<tr>\n<td>Constructors<\/td>\n<td>Functions<\/td>\n<\/tr>\n<tr>\n<td>Methods<\/td>\n<td>Functions<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<p>But first, why do we care about inheritance at all? There are primarily two<br \/>\nreasons. The first is type convenience. We want the language system to<br \/>\nautomatically <i>cast<\/i> references of similar classes. Little type-safety is<br \/>\nobtained from a type system which requires the routine explicit  casting of<br \/>\nobject references. This is of critical importance in strongly-typed languages,<br \/>\nbut it is irrelevant in loosely-typed languages like JavaScript, where object<br \/>\nreferences  never need casting.<\/p>\n<p>The second reason is code reuse. It is very common to have a quantity of objects<br \/>\n  all implementing exactly the same methods. Classes make it possible to create<br \/>\n  them all from a single set of definitions. It is also common to have objects<br \/>\n  that are similar to some other objects, but differing only in the addition or<br \/>\n  modification of a small number of methods. Classical inheritance is useful for<br \/>\n  this but prototypal inheritance is even more useful. <\/p>\n<p>To demonstrate this, we will introduce a little <a href=\"#sugar\">sugar<\/a><br \/>\n  which will let us write in a style that resembles a conventional classical language.<br \/>\n  We will then show useful patterns which are not available in classical languages.<br \/>\n  Then finally, we will explain the <a href=\"#sugar\">sugar<\/a>. <\/p>\n<h2>Classical Inheritance<\/h2>\n<p>First, we will make a <tt>Parenizor<\/tt> class that will have  set and get<br \/>\nmethods for its <tt>value<\/tt>, and a <tt>toString<\/tt> method that will wrap<br \/>\nthe <tt>value<\/tt> in parens. <\/p>\n<pre>function Parenizor(value) {\n    this.setValue(value);\n}\n\nParenizor.method('setValue', function (value) {\n    this.value = value;\n    return this;\n});\n\nParenizor.method('getValue', function () {\n    return this.value;\n});\n\nParenizor.method('toString', function () {\n    return '(' + this.getValue() + ')';\n});\n<\/pre>\n<p>The syntax is a little unusual, but it is easy to recognize the classical<br \/>\npattern in it. The <tt>method<\/tt> method takes a method name and a function,<br \/>\nadding them to the class as a public method.<\/p>\n<p>So now we can write<\/p>\n<pre>myParenizor = new Parenizor(0);\nmyString = myParenizor.toString();<\/pre>\n<p>As you would expect, <tt>myString<\/tt> is <tt>\"(0)\"<\/tt>.<\/p>\n<p>Now we will make another class which will inherit from <tt>Parenizor<\/tt>,<br \/>\nwhich is  the same except that its <tt>toString<\/tt> method will<br \/>\nproduce <tt>\"-0-\"<\/tt> if the <tt>value<\/tt> is zero or empty.<\/p>\n<pre>function ZParenizor(value) {\n    this.setValue(value);\n}\n\nZParenizor.inherits(Parenizor);\n\nZParenizor.method('toString', function () {\n    if (this.getValue()) {\n        return this.uber('toString');\n    }\n    return \"-0-\";\n});\n<\/pre>\n<p>The <tt>inherits<\/tt> method is similar to Java&#8217;s <tt>extends<\/tt>.<br \/>\nThe <tt>uber<\/tt> method is similar to Java&#8217;s <tt>super<\/tt>. It lets a method<br \/>\ncall a method of  the parent class. (The names have been changed to avoid<br \/>\nreserved word restrictions.)<\/p>\n<p>So now we can write<\/p>\n<pre>myZParenizor = new ZParenizor(0);\nmyString = myZParenizor.toString();<\/pre>\n<p>This time, <tt>myString<\/tt> is <tt>\"-0-\"<\/tt>.<\/p>\n<p>JavaScript does not have classes, but we can program as though it does.<\/p>\n<h2>Multiple Inheritance<\/h2>\n<p>By manipulating a function&#8217;s <tt>prototype<\/tt> object, we can implement<br \/>\nmultiple inheritance, allowing us to make a class built from the methods of<br \/>\nmultiple classes.  Promiscuous multiple inheritance can be difficult to<br \/>\nimplement and can potentially suffer from method name collisions. We could<br \/>\nimplement promiscuous multiple inheritance in JavaScript, but for this example<br \/>\nwe will use a more disciplined form<br \/>\ncalled <i><a href=\"https:\/\/www.cosmik.com\/aa-october99\/stan_freberg.html\">Swiss<br \/>\nInheritance<\/a><\/i>.<\/p>\n<p>Suppose there is a <tt>NumberValue<\/tt> class that has a <tt>setValue<\/tt><br \/>\nmethod that checks that the <tt>value<\/tt> is a number in a certain range,<br \/>\nthrowing an exception if necessary. We only want<br \/>\nits <tt>setValue<\/tt> and <tt>setRange<\/tt> methods for our <tt>ZParenizor<\/tt>.<br \/>\nWe certainly don&#8217;t want its <tt>toString<\/tt> method. So, we write<\/p>\n<pre>ZParenizor.swiss(NumberValue, 'setValue', 'setRange');<\/pre>\n<p>This adds only the requested methods to our class.<\/p>\n<h2>Parasitic Inheritance<\/h2>\n<p>There is another way to write <tt>ZParenizor<\/tt>. Instead of inheriting<br \/>\nfrom <tt>Parenizor<\/tt>, we write a constructor that calls<br \/>\nthe <tt>Parenizor<\/tt> constructor, passing off the result as its own.<br \/>\nAnd instead of adding public methods,  the constructor adds  privileged methods.<\/p>\n<pre>function ZParenizor2(value) {\n    var that = new Parenizor(value);\n    that.toString = function () {\n        if (this.getValue()) {\n            return this.uber('toString');\n        }\n        return \"-0-\"\n    };\n    return that;\n}\n<\/pre>\n<p>Classical inheritance is about the <i>is-a<\/i> relationship, and parasitic<br \/>\n  inheritance is about the <i>was-a-but-now&#8217;s-a<\/i> relationship. The constructor<br \/>\n  has a larger role in the construction of the object. Notice that the <tt>uber<\/tt><br \/>\n  n\u00e9e <tt>super<\/tt> method is still available to the privileged methods. <\/p>\n<h2>Class Augmentation<\/h2>\n<p>JavaScript&#8217;s dynamism allows us to add or replace methods of an existing class.<br \/>\n  We can call the <tt>method<\/tt> method at any time, and all present and future<br \/>\n  instances of the class will have that method. We can literally extend a class<br \/>\n  at any time. Inheritance works retroactively. We call this <i>Class Augmentation<\/i><br \/>\n  to avoid confusion with Java&#8217;s <tt>extends<\/tt>, which means something else.<\/p>\n<h2>Object Augmentation<\/h2>\n<p>In the static object-oriented languages, if you want an object which is slightly<br \/>\n  different than another object, you need to define a new class. In JavaScript,<br \/>\n  you can add methods to individual objects without the need for additional classes.<br \/>\n  This has enormous power because you can write far fewer classes and the classes<br \/>\n  you do write can be much simpler. Recall that JavaScript objects are like hashtables.<br \/>\n  You can add new values at any time. If the value is a function, then it becomes<br \/>\n  a method. <\/p>\n<p>So in the example above, I didn&#8217;t need a <tt>ZParenizor<\/tt> class at all.<br \/>\nI could have simply modified my instance.<\/p>\n<pre>myParenizor = new Parenizor(0);\nmyParenizor.toString = function () {\n    if (this.getValue()) {\n        return this.uber('toString');\n    }\n    return \"-0-\";\n};\nmyString = myParenizor.toString();<\/pre>\n<p>We added a <tt>toString<\/tt> method to our <tt>myParenizor<\/tt> instance without<br \/>\n  using any form of inheritance. We can evolve individual instances because the<br \/>\n  language is class-free. <\/p>\n<h2 id=\"sugar\">Sugar<\/h2>\n<p>To make the examples above work, I wrote four <a href=\"https:\/\/en.wikipedia.org\/wiki\/Syntactic_sugar\">sugar<\/a><br \/>\n  methods. First, the <tt>method<\/tt> method, which adds an instance method to<br \/>\n  a class. <\/p>\n<pre>Function.prototype.method = function (name, func) {\n    this.prototype[name] = func;\n    return this;\n};<\/pre>\n<p>This adds a public method to the <tt>Function.prototype<\/tt>, so all<br \/>\nfunctions get it by Class Augmentation. It takes a name and a function, and<br \/>\nadds them to a function&#8217;s <tt>prototype<\/tt> object. <\/p>\n<p>It returns <tt>this<\/tt>. When I write a method that doesn&#8217;t need to return<br \/>\n  a value, I usually have it return <tt>this<\/tt>. It allows for a cascade-style<br \/>\n  of programming. <\/p>\n<p>Next comes the <tt>inherits<\/tt> method, which indicates that one class inherits<br \/>\n  from another. It should be called after both classes are defined, but before<br \/>\n  the inheriting class&#8217;s methods are added. <\/p>\n<pre>Function.method('inherits', function (parent) {\n    this.prototype = new parent();\n    var d = {}, \n        p = this.prototype;\n    this.prototype.constructor = parent; \n    this.method('uber', function uber(name) {\n        if (!(name in d)) {\n            d[name] = 0;\n        }        \n        var f, r, t = d[name], v = parent.prototype;\n        if (t) {\n            while (t) {\n                v = v.constructor.prototype;\n                t -= 1;\n            }\n            f = v[name];\n        } else {\n            f = p[name];\n            if (f == this[name]) {\n                f = v[name];\n            }\n        }\n        d[name] += 1;\n        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));\n        d[name] -= 1;\n        return r;\n    });\n    return this;\n});<\/pre>\n<p>Again, we augment <tt>Function<\/tt>. We make an instance of the<br \/>\n<tt>parent<\/tt> class and use it as the new <tt>prototype<\/tt>. We also<br \/>\ncorrect the <tt>constructor<\/tt> field, and we add the <tt>uber<\/tt> method to<br \/>\nthe <tt>prototype<\/tt> as well.<\/p>\n<p>The <tt>uber<\/tt> method looks for the named method in its own <tt>prototype<\/tt>.<br \/>\n  This is the function to invoke in the case of Parasitic Inheritance or Object<br \/>\n  Augmentation. If we are doing Classical Inheritance, then we need to find the<br \/>\n  function in the <tt>parent<\/tt>&#8216;s <tt>prototype<\/tt>. The <tt>return<\/tt> statement<br \/>\n  uses the function&#8217;s <tt>apply<\/tt> method to invoke the function, explicitly<br \/>\n  setting <tt>this<\/tt> and passing an array of parameters. The parameters (if<br \/>\n  any) are obtained from the <tt>arguments<\/tt> array. Unfortunately, the<br \/>\n  <tt>arguments<\/tt><br \/>\n  array is not a true array, so we have to use <tt>apply<\/tt> again to invoke<br \/>\n  the array <tt>slice<\/tt> method. <\/p>\n<p>Finally, the <tt>swiss<\/tt> method.<\/p>\n<pre>Function.method('swiss', function (parent) {\n    for (var i = 1; i &lt; arguments.length; i += 1) {\n        var name = arguments[i];\n        this.prototype[name] = parent.prototype[name];\n    }\n    return this;\n});<\/pre>\n<p>The <tt>swiss<\/tt> method loops through the <tt>arguments<\/tt>.<br \/>\nFor each <tt>name<\/tt>, it copies a member from the <tt>parent<\/tt>&#8216;s<br \/>\n<tt>prototype<\/tt> to the new class&#8217;s <tt>prototype<\/tt>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript&#8217;s prototypal inheritance has more expressive power than classical inheritance, as we will see presently. Java JavaScript Strongly-typed Loosely-typed Static Dynamic Classical Prototypal Classes Functions Constructors Functions Methods Functions But first, why do we care about inheritance at all? There are primarily two reasons. The first is type convenience. We want the language system to automatically cast references&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_has_post_settings":[],"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[17],"tags":[],"hashtags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Classical Inheritance in JavaScript - PureSourceCode<\/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:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Classical Inheritance in JavaScript - PureSourceCode\" \/>\n<meta property=\"og:description\" content=\"JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript&#8217;s prototypal inheritance has more expressive power than classical inheritance, as we will see presently. Java JavaScript Strongly-typed Loosely-typed Static Dynamic Classical Prototypal Classes Functions Constructors Functions Methods Functions But first, why do we care about inheritance at all? There are primarily two reasons. The first is type convenience. We want the language system to automatically cast references&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"PureSourceCode\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/puresourcecode\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/henryLdn\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-22T11:38:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/03\/cropped-cropped-top_1600-1.jpg?fit=1900%2C600&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1900\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Enrico\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@erossiniuk\" \/>\n<meta name=\"twitter:site\" content=\"@puresourcecode\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Enrico\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/\"},\"author\":{\"name\":\"Enrico\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#\\\/schema\\\/person\\\/d0ff2e1c52b0d470af84246fd09f1f16\"},\"headline\":\"Classical Inheritance in JavaScript\",\"datePublished\":\"2015-07-22T11:38:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/\"},\"wordCount\":1118,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#organization\"},\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/\",\"url\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/\",\"name\":\"Classical Inheritance in JavaScript - PureSourceCode\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#website\"},\"datePublished\":\"2015-07-22T11:38:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/javascript\\\/classical-inheritance-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/puresourcecode.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Classical Inheritance in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#website\",\"url\":\"https:\\\/\\\/puresourcecode.com\\\/\",\"name\":\"PureSourceCode.com\",\"description\":\"All technologies, only pure source code\",\"publisher\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/puresourcecode.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#organization\",\"name\":\"PureSourceCode\",\"url\":\"https:\\\/\\\/puresourcecode.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/puresourcecode.com\\\/wp-content\\\/uploads\\\/job-manager-uploads\\\/company_logo\\\/2021\\\/05\\\/psc_logo.png?fit=512%2C512&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/puresourcecode.com\\\/wp-content\\\/uploads\\\/job-manager-uploads\\\/company_logo\\\/2021\\\/05\\\/psc_logo.png?fit=512%2C512&ssl=1\",\"width\":512,\"height\":512,\"caption\":\"PureSourceCode\"},\"image\":{\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/puresourcecode\",\"https:\\\/\\\/x.com\\\/puresourcecode\",\"https:\\\/\\\/www.youtube.com\\\/erossini?sub_confirmation=1\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/puresourcecode.com\\\/#\\\/schema\\\/person\\\/d0ff2e1c52b0d470af84246fd09f1f16\",\"name\":\"Enrico\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a41f0bf0ce51e827836b745ab21dabdb477fc4a05f80632fb9a96a76a72d26c1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a41f0bf0ce51e827836b745ab21dabdb477fc4a05f80632fb9a96a76a72d26c1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a41f0bf0ce51e827836b745ab21dabdb477fc4a05f80632fb9a96a76a72d26c1?s=96&d=mm&r=g\",\"caption\":\"Enrico\"},\"description\":\"My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)\",\"sameAs\":[\"https:\\\/\\\/puresourcecode.com\",\"https:\\\/\\\/www.facebook.com\\\/henryLdn\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/rossiniuk\\\/\",\"https:\\\/\\\/x.com\\\/erossiniuk\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UC2jeteqpm3sUDqQpKGqpCLg\",\"https:\\\/\\\/www.tumblr.com\\\/blog\\\/erossiniuk\"],\"url\":\"https:\\\/\\\/puresourcecode.com\\\/author\\\/enrico\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Classical Inheritance in JavaScript - PureSourceCode","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:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Classical Inheritance in JavaScript - PureSourceCode","og_description":"JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript&#8217;s prototypal inheritance has more expressive power than classical inheritance, as we will see presently. Java JavaScript Strongly-typed Loosely-typed Static Dynamic Classical Prototypal Classes Functions Constructors Functions Methods Functions But first, why do we care about inheritance at all? There are primarily two reasons. The first is type convenience. We want the language system to automatically cast references&hellip;","og_url":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/","og_site_name":"PureSourceCode","article_publisher":"https:\/\/www.facebook.com\/puresourcecode","article_author":"https:\/\/www.facebook.com\/henryLdn","article_published_time":"2015-07-22T11:38:00+00:00","og_image":[{"width":1900,"height":600,"url":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/03\/cropped-cropped-top_1600-1.jpg?fit=1900%2C600&ssl=1","type":"image\/jpeg"}],"author":"Enrico","twitter_card":"summary_large_image","twitter_creator":"@erossiniuk","twitter_site":"@puresourcecode","twitter_misc":{"Written by":"Enrico","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/#article","isPartOf":{"@id":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/"},"author":{"name":"Enrico","@id":"https:\/\/puresourcecode.com\/#\/schema\/person\/d0ff2e1c52b0d470af84246fd09f1f16"},"headline":"Classical Inheritance in JavaScript","datePublished":"2015-07-22T11:38:00+00:00","mainEntityOfPage":{"@id":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/"},"wordCount":1118,"commentCount":0,"publisher":{"@id":"https:\/\/puresourcecode.com\/#organization"},"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/","url":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/","name":"Classical Inheritance in JavaScript - PureSourceCode","isPartOf":{"@id":"https:\/\/puresourcecode.com\/#website"},"datePublished":"2015-07-22T11:38:00+00:00","breadcrumb":{"@id":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/puresourcecode.com\/javascript\/classical-inheritance-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/puresourcecode.com\/"},{"@type":"ListItem","position":2,"name":"Classical Inheritance in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/puresourcecode.com\/#website","url":"https:\/\/puresourcecode.com\/","name":"PureSourceCode.com","description":"All technologies, only pure source code","publisher":{"@id":"https:\/\/puresourcecode.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/puresourcecode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/puresourcecode.com\/#organization","name":"PureSourceCode","url":"https:\/\/puresourcecode.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/puresourcecode.com\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/job-manager-uploads\/company_logo\/2021\/05\/psc_logo.png?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/job-manager-uploads\/company_logo\/2021\/05\/psc_logo.png?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"PureSourceCode"},"image":{"@id":"https:\/\/puresourcecode.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/puresourcecode","https:\/\/x.com\/puresourcecode","https:\/\/www.youtube.com\/erossini?sub_confirmation=1"]},{"@type":"Person","@id":"https:\/\/puresourcecode.com\/#\/schema\/person\/d0ff2e1c52b0d470af84246fd09f1f16","name":"Enrico","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a41f0bf0ce51e827836b745ab21dabdb477fc4a05f80632fb9a96a76a72d26c1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a41f0bf0ce51e827836b745ab21dabdb477fc4a05f80632fb9a96a76a72d26c1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a41f0bf0ce51e827836b745ab21dabdb477fc4a05f80632fb9a96a76a72d26c1?s=96&d=mm&r=g","caption":"Enrico"},"description":"My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)","sameAs":["https:\/\/puresourcecode.com","https:\/\/www.facebook.com\/henryLdn","https:\/\/www.linkedin.com\/in\/rossiniuk\/","https:\/\/x.com\/erossiniuk","https:\/\/www.youtube.com\/channel\/UC2jeteqpm3sUDqQpKGqpCLg","https:\/\/www.tumblr.com\/blog\/erossiniuk"],"url":"https:\/\/puresourcecode.com\/author\/enrico\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":3667,"url":"https:\/\/puresourcecode.com\/javascript\/typescript-javascript-made-easier-and-simpler\/","url_meta":{"origin":132,"position":0},"title":"TypeScript: JavaScript made Easier and Simpler","author":"Enrico","date":"June 6, 2020","format":false,"excerpt":"TypeScript is a language that is strongly typed and object-oriented. TypeScript is a combination of JavaScript and Type Annotations","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/puresourcecode.com\/category\/javascript\/"},"img":{"alt_text":"typescript wallpaper","src":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/06\/typescript-wallpaper.png?fit=1200%2C553&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/06\/typescript-wallpaper.png?fit=1200%2C553&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/06\/typescript-wallpaper.png?fit=1200%2C553&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/06\/typescript-wallpaper.png?fit=1200%2C553&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2020\/06\/typescript-wallpaper.png?fit=1200%2C553&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":43822,"url":"https:\/\/puresourcecode.com\/programming-languages\/python\/getting-started-with-python\/","url_meta":{"origin":132,"position":1},"title":"Getting started with Python","author":"Enrico","date":"March 6, 2023","format":false,"excerpt":"Python is a high-level, general-purpose and a very popular programming language that lets you work quickly and integrate systems effectively","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/puresourcecode.com\/category\/programming-languages\/python\/"},"img":{"alt_text":"Python programming language - logo","src":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2023\/03\/python.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2023\/03\/python.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2023\/03\/python.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2023\/03\/python.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2023\/03\/python.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":87,"url":"https:\/\/puresourcecode.com\/net7\/what-is-the-javascript-equivalent-of-print-r-in-php\/","url_meta":{"origin":132,"position":2},"title":"What is the JavaScript Equivalent of print_r in PHP?","author":"Enrico","date":"January 15, 2015","format":false,"excerpt":"What is the JavaScript Equivalent of PHP print_r() function? In other words, how you can \"print\" a javascript object in a way that's readable by humans? Code You could use JSON.stringify, as following: The HTML part Let's create two links for demo:<p><a id=\"print_demo\" href=\"javascript:void(0);\">Print object<\/a><\/p><p><a id=\"pretty_print_demo\" href=\"javascript:void(0);\">Pretty Print object<\/a> The\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":44071,"url":"https:\/\/puresourcecode.com\/dotnet\/blazor\/custom-javascript-function-in-blazor\/","url_meta":{"origin":132,"position":3},"title":"Custom JavaScript function in Blazor","author":"Enrico","date":"April 3, 2023","format":false,"excerpt":"In this new post custom JavaScript function in Blazor, I present how to create functions in C# in a Blazor page and integrate them with JavaScript","rel":"","context":"In &quot;Blazor&quot;","block_context":{"text":"Blazor","link":"https:\/\/puresourcecode.com\/category\/dotnet\/blazor\/"},"img":{"alt_text":"Microsoft Blazor wallpaper","src":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2021\/04\/blazor-wallpaper.jpg?fit=1200%2C625&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2021\/04\/blazor-wallpaper.jpg?fit=1200%2C625&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2021\/04\/blazor-wallpaper.jpg?fit=1200%2C625&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2021\/04\/blazor-wallpaper.jpg?fit=1200%2C625&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2021\/04\/blazor-wallpaper.jpg?fit=1200%2C625&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":60780,"url":"https:\/\/puresourcecode.com\/javascript\/new-markdowneditor-components-for-javascript-and-blazor\/","url_meta":{"origin":132,"position":4},"title":"New MarkdownEditor components for JavaScript and Blazor","author":"Enrico","date":"July 1, 2024","format":false,"excerpt":"Today, after 2 years, I released a new Markdown Editor components for JavaScript and Blazor. The full source code is available on GitHub.","rel":"","context":"In &quot;.NET8&quot;","block_context":{"text":".NET8","link":"https:\/\/puresourcecode.com\/category\/dotnet\/net8\/"},"img":{"alt_text":"markdown editor blazor logo","src":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/01\/markdown-editor-blazor-logo.jpg?fit=1200%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/01\/markdown-editor-blazor-logo.jpg?fit=1200%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/01\/markdown-editor-blazor-logo.jpg?fit=1200%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/01\/markdown-editor-blazor-logo.jpg?fit=1200%2C800&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/01\/markdown-editor-blazor-logo.jpg?fit=1200%2C800&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":30648,"url":"https:\/\/puresourcecode.com\/tools\/validate-json-with-postman\/","url_meta":{"origin":132,"position":5},"title":"Validate JSON with Postman","author":"Enrico","date":"March 8, 2022","format":false,"excerpt":"In this new post, I explain how to validate a JSON result with Postman from your APIs using JavaScript with few lines of code. Full code here","rel":"","context":"In &quot;Tools&quot;","block_context":{"text":"Tools","link":"https:\/\/puresourcecode.com\/category\/tools\/"},"img":{"alt_text":"postman","src":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/03\/postman.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/03\/postman.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/03\/postman.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/03\/postman.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/puresourcecode.com\/wp-content\/uploads\/2022\/03\/postman.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_shortlink":"https:\/\/wp.me\/pbPHZq-28","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":0,"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"wp:attachment":[{"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/tags?post=132"},{"taxonomy":"hashtags","embeddable":true,"href":"https:\/\/puresourcecode.com\/wp-json\/wp\/v2\/hashtags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}