{"id":1022,"date":"2022-08-30T15:11:05","date_gmt":"2022-08-30T15:11:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-functional-inheritance-with-prototypes-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:05","modified_gmt":"2022-08-30T15:11:05","slug":"javascript-functional-inheritance-with-prototypes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-functional-inheritance-with-prototypes-collection-of-common-programming-errors\/","title":{"rendered":"Javascript functional inheritance with prototypes-Collection of common programming errors"},"content":{"rendered":"<p>To proper use Javascript-prototype based inheritance you could use <code>fastClass<\/code> https:\/\/github.com\/dotnetwise\/Javascript-FastClass<\/p>\n<p>You have the simpler <code>inheritWith<\/code> flavor:<\/p>\n<pre><code>  var Mammal = function (spec) {\n    this.spec = spec;\n}.define({\n    clearThroat: function () { return \"Ahem\" },\n    getName: function () {\n        return this.spec.name;\n    },\n    says: function () {\n        return this.clearThroat() + ' ' + spec.saying || '';\n    }\n});\n\nvar Cat = Mammal.inheritWith(function (base, baseCtor) {\n    return {\n        constructor: function(spec) { \n            spec = spec || {};\n            baseCtor.call(this, spec); \n        },\n        purr: function() {\n            return this.clearThroat() + \" purr\";\n        },\n        getName: function() {\n            return this.says() + ' ' + this.spec.name + this.says();\n        }\n    }\n});\n\nvar kitty = new Cat({ name: \"Fluffy\" });\nkitty.purr(); \/\/ Ahem purr\nkitty.getName(); \/\/ Ahem Fluffy Ahem\n<\/code><\/pre>\n<p>And if you are very concerned about performance then you have the <code>fastClass<\/code> flavor:<\/p>\n<pre><code>var Mammal = function (spec) {\n    this.spec = spec;\n}.define({\n    clearThroat: function () { return \"Ahem\" },\n    getName: function () {\n        return this.spec.name;\n    },\n    says: function () {\n        return this.clearThroat() + ' ' + spec.saying || '';\n    }\n});\n\nvar Cat = Mammal.fastClass(function (base, baseCtor) {\n    return function() {\n        this.constructor = function(spec) { \n            spec = spec || {};\n            baseCtor.call(this, spec); \n        };\n        this.purr = function() {\n            return this.clearThroat() + \" purr\";\n        },\n        this.getName = function() {\n            return this.says() + ' ' + this.spec.name + this.says();\n        }\n    }\n});\n\nvar kitty = new Cat({ name: \"Fluffy\" });\nkitty.purr(); \/\/ Ahem purr\nkitty.getName(); \/\/ Ahem Fluffy Ahem\n<\/code><\/pre>\n<p>Btw, your initial code doesn&#8217;t make any sense but I have respected it literally.<\/p>\n<p><code>fastClass<\/code> utility:<\/p>\n<pre><code>Function.prototype.fastClass = function (creator) {\n    var baseClass = this, ctor = (creator || function () { this.constructor = function () { baseClass.apply(this, arguments); } })(this.prototype, this)\n\n    var derrivedProrotype = new ctor();\n\n    if (!derrivedProrotype.hasOwnProperty(\"constructor\"))\n        derrivedProrotype.constructor = function () { baseClass.apply(this, arguments); }\n\n    derrivedProrotype.constructor.prototype = derrivedProrotype;\n    return derrivedProrotype.constructor;\n};\n<\/code><\/pre>\n<p><code>inheritWith<\/code> utility:<\/p>\n<pre><code>Function.prototype.inheritWith = function (creator, makeConstructorNotEnumerable) {\n    var baseCtor = this;\n    var creatorResult = creator.call(this, this.prototype, this) || {};\n    var Derrived = creatorResult.constructor ||\n    function defaultCtor() {\n        baseCtor.apply(this, arguments);\n    }; \n    var derrivedPrototype;\n    function __() { };\n    __.prototype = this.prototype;\n    Derrived.prototype = derrivedPrototype = new __;\n\n    for (var p in creatorResult)\n        derrivedPrototype[p] = creatorResult[p];\n\n    if (makeConstructorNotEnumerable &amp;&amp; canDefineNonEnumerableProperty) \/\/this is not default as it carries over some performance overhead\n        Object.defineProperty(derrivedPrototype, 'constructor', {\n            enumerable: false,\n            value: Derrived\n        });\n\n    return Derrived;\n};\n<\/code><\/pre>\n<p><code>define<\/code> utility:<\/p>\n<pre><code>Function.prototype.define = function (prototype) {\n    var extendeePrototype = this.prototype;\n    if (prototype)\n        for (var p in prototype)\n            extendeePrototype[p] = prototype[p];\n    return this;\n}\n<\/code><\/pre>\n<p>[* Disclaimer, I am the author of the open source package and the names of the methods themselves might be renamed in future` *]<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:14:41. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>To proper use Javascript-prototype based inheritance you could use fastClass https:\/\/github.com\/dotnetwise\/Javascript-FastClass You have the simpler inheritWith flavor: var Mammal = function (spec) { this.spec = spec; }.define({ clearThroat: function () { return &#8220;Ahem&#8221; }, getName: function () { return this.spec.name; }, says: function () { return this.clearThroat() + &#8216; &#8216; + spec.saying || &#8221;; } [&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-1022","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1022","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=1022"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1022\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}