{"id":862,"date":"2022-08-30T15:08:25","date_gmt":"2022-08-30T15:08:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/requirejs-and-prototypal-inheritance-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:25","modified_gmt":"2022-08-30T15:08:25","slug":"requirejs-and-prototypal-inheritance-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/requirejs-and-prototypal-inheritance-collection-of-common-programming-errors\/","title":{"rendered":"RequireJS and Prototypal Inheritance-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m running into an issue with using RequireJS and Prototypal inheritance. Here&#8217;s my module:<\/p>\n<pre><code>define(function () {\n  function Module(data) {\n    this.data = data;\n  }\n\n  Module.prototype.getData = function () {\n    return this.data;\n  };\n\n  Module.prototype.doSomething = function () {\n    console.log(this.data);\n    console.log(this.getData());\n  };\n\n  return Module;\n\n  Module.prototype.callFunction = function (fn) {\n    if (this[fn]) {\n      console.log('call');\n      Module.prototype[fn]();\n    }\n  };\n});\n<\/code><\/pre>\n<p>Then I instantiate the module, like so:<\/p>\n<pre><code>var module = new Module({ name: 'Marty' });\nmodule.getData(); \/\/ returns { name: 'Marty' }\nmodule.data; \/\/ returns { name: 'Marty' }\nmodule.callFunction('doSomething') \/\/ returns undefined on the first (and second) console log\n<\/code><\/pre>\n<p>The <code>console.log<\/code>s in the <code>module.doSomething()<\/code> always return <code>undefined<\/code>. Am I misunderstanding how prototypal inheritance works with RequireJS?<\/p>\n<ol>\n<li>\n<p>As it turns out, I had written the callFunction method incorrectly. The correct way is:<\/p>\n<pre><code>Module.prototype.callFunction = function (fn) {\n  if (this[fn] &amp;&amp; typeof this[fn] === \"function\") {\n    this[fn]();\n  }\n};\n<\/code><\/pre>\n<p>The problem was using <code>Module.prototype<\/code> instead of <code>this<\/code>. Whoops.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:49:23. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m running into an issue with using RequireJS and Prototypal inheritance. Here&#8217;s my module: define(function () { function Module(data) { this.data = data; } Module.prototype.getData = function () { return this.data; }; Module.prototype.doSomething = function () { console.log(this.data); console.log(this.getData()); }; return Module; Module.prototype.callFunction = function (fn) { if (this[fn]) { console.log(&#8216;call&#8217;); Module.prototype[fn](); } }; }); [&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-862","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/862","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=862"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/862\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}