{"id":1187,"date":"2022-08-30T15:13:50","date_gmt":"2022-08-30T15:13:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/module-prototype-and-multiple-instances-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:50","modified_gmt":"2022-08-30T15:13:50","slug":"module-prototype-and-multiple-instances-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/module-prototype-and-multiple-instances-collection-of-common-programming-errors\/","title":{"rendered":"Module\/prototype and multiple instances-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to get a grip on &#8220;OOP&#8221; JavaScript techniques, and I began writing a small test application today. Basically, it&#8217;s a game loop and on each update coordinates are to be increased so that an HTML element moves.<\/p>\n<p>The problem is I want to be able to run more than one instance of the app, and therefore I&#8217;m trying to store the instance data in <code>this<\/code>, but what is saved in my constructor and <code>exec()<\/code> method is not available in the private <code>update()<\/code> method. What seems to be the officer, problem?<\/p>\n<pre><code>var Jsloth = (function () {\n    var Jsloth = function () {\n        var sloth = document.createElement('div');\n        var attr = document.createAttribute('class');\n        attr.value = 'sloth';\n        sloth.setAttributeNode(attr);\n        this.sloth = document.getElementsByTagName('body')[0].appendChild(sloth);\n    };\n\n    var exec = function () {\n        this.x = 0;\n        this.y = 0;\n        var that = this;\n        setInterval(function () {\n            that.update();\n        }, 1000\/10);\n    };\n\n    var update = function () {\n        this.x++;\n        this.y++;\n        this.sloth.style.left = this.x + 'px';\n        this.sloth.style.bottom = this.y + 'px';\n    };\n\n    Jsloth.prototype.constructor = Jsloth;\n    Jsloth.prototype.exec = exec;\n    Jsloth.prototype.update = update;\n\n    return Jsloth;\n})();\n\nvar sloth1 = new Jsloth();\nsloth1.exec();\n<\/code><\/pre>\n<p><strong>Edit:<\/strong> Updated code with a working solution!<\/p>\n<ol>\n<li>\n<p>You&#8217;ve not added <code>update<\/code> to the prototype. The value of <code>this<\/code> in that method will be most likely the <code>window<\/code> object.<\/p>\n<p>Change your call from this:<\/p>\n<pre><code>update();\n<\/code><\/pre>\n<p>To this:<\/p>\n<pre><code>update.call(this);\n<\/code><\/pre>\n<p>Or add <code>update<\/code> to the <code>.prototype<\/code>:<\/p>\n<pre><code>Jsloth.prototype.update = update;\n<\/code><\/pre>\n<p>and use:<\/p>\n<pre><code>this.update();\n<\/code><\/pre>\n<p>But if you&#8217;re going to call <code>update()<\/code> from the <code>setInterval()<\/code>, you&#8217;ll need to ensure the proper <code>this<\/code> value.<\/p>\n<p>To do that, you can pass an anonymous function, and keep a reference to the outer <code>this<\/code> value in a variable.<\/p>\n<pre><code>var exec = function () {\n    this.x = 0;\n    this.y = 0;\n    var that = this;\n    setInterval(function() {\n        that.update();\n      \/\/update.call(that); \/\/ if you didn't add update() to the .prototype\n    }, 1000\/10);\n};\n<\/code><\/pre>\n<\/li>\n<li>\n<p>In JavaScript, <code>this<\/code> is (almost) entirely decided by <em>how<\/em> you call a function, not where\/how it&#8217;s defined.<\/p>\n<p>The way you&#8217;re calling <code>update<\/code>:<\/p>\n<pre><code>update();\n<\/code><\/pre>\n<p>&#8230;<code>this<\/code> will be the global object (<code>window<\/code> on browsers), or <code>undefined<\/code> if you use <code>\"use strict\"<\/code>.<\/p>\n<p>To set <code>this<\/code> within <code>update<\/code>, use <code>call<\/code> or <code>apply<\/code>:<\/p>\n<pre><code>update.call(this);\n\/\/ or\nupdate.apply(this);\n<\/code><\/pre>\n<p>More <em>(on my blog)<\/em>:<\/p>\n<ul>\n<li><em>Mythical methods<\/em><\/li>\n<li><em>You must remember <code>this<\/code><\/em><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:38:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to get a grip on &#8220;OOP&#8221; JavaScript techniques, and I began writing a small test application today. Basically, it&#8217;s a game loop and on each update coordinates are to be increased so that an HTML element moves. The problem is I want to be able to run more than one instance of the [&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-1187","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1187","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=1187"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}