Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a classic Javascript stumbling block, relating to how the "this" key is scoped within a closure. To illustrate:</p> <pre><code>redPrinter = function() { this.X = 'red'; return function() { return this.X; } } main = function() { this.X = 'blue'; var myRedPrinter = new redPrinter(); alert("Red printer returned: " + myRedPrinter()); } main(); </code></pre> <p>This code will print out:</p> <pre><code>Red printer returned: blue </code></pre> <p>because the scope of 'this' in the line:</p> <pre><code>return this.X </code></pre> <p>is actually tied to the main() object at the time of invocation.</p> <p><strong>There are generally 2 ways to address this:</strong></p> <p><strong>1)</strong> Avoid using the 'this' keyword within a function closure. To fix your code this way, I would simply collect all of the event bindings in one place instead of cascading them, thus removing the 'this' references from both 'displaydown()' and 'displaymove()':</p> <pre><code>test.prototype = { init: function () { addEvent(document, 'mousedown', this.displaydown); addEvent(document, 'mousemove', this.displaymove); }, displaydown : function(e){ document.getElementById('mydiv').innerHTML = "down"; }, displaymove : function(e){ document.getElementById('mydiv').innerHTML = "move"; } } </code></pre> <p>OR</p> <p><strong>2)</strong> Use function currying to bind the scope at definition time. I've lifted the bind() method from the Prototype library to illustrate:</p> <pre><code>// declare the global bind() method for all functions Function.prototype.bind = function(obj) { var method = this, temp = function() { return method.apply(obj, arguments); }; return temp; } test.prototype = { init: function () { addEvent(document, 'mousedown', this.displaydown); }, displaydown : function(e){ document.getElementById('mydiv').innerHTML = "down"; // note that we bind the method object here addEvent(document, 'mousemove', this.displaymove.bind(this)); }, displaymove : function(e){ document.getElementById('mydiv').innerHTML = "move"; } } </code></pre> <p>The important change here is:</p> <pre><code>this.displaymove.bind(this) </code></pre> <p>which basically says, "when you call displaymove(), re-scope the keyword 'this' to the original scope context instead of the current Event object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload