Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript "this" pointer in Method Invocation Pattern not pointing to object
    primarykey
    data
    text
    <p>I am attempting to use the method invocation pattern in Javascript. I declare a function as an object member.</p> <p>According to <strong>Javascript: The Good Parts</strong>, this should result in the <code>this</code> pointer referencing the enclosing object. When I've tried this before, this has been the case.</p> <p>In the sample of code below, the <code>this</code> pointer reference by the single <code>console.log</code> statement is pointing to the <em>function</em>, not the object. I've double-checked my code and I honestly don't know what's going on.</p> <p>I could use another pair of eyes on this. Is there something really obvious that I'm missing here, or am I expecting the wrong behavior? Thank you.</p> <p>EDIT: I had a mistake in my code that I posted (it's been in flux); the keyword inside the anonymous function should be <code>that</code>, not <code>this</code>. Fixed.</p> <p>DOUBLE EDIT: I've added the rest of my code within the module. I'm trying to write a commonJS module (in accordance with the gameJS library that I'm using) and although I'm not sure where that would be the problem, I'm wondering if it is. Does this change anything?</p> <pre><code>var gamejs = require('gamejs'); var system = require('app/system'); var input = { eval_keys: function () { console.log(this); // This should be the outer object, but shows the function! var that = this; gamejs.event.get().forEach(function (event) { if (event.type === gamejs.event.KEY_DOWN) { for (var key in that.keyconfig) { if (that.keyconfig.hasOwnProperty(key)) { if (event.key === gamejs.event[key]) { that.keyconfig.key = true; } } } system.log("KEYDOWN", event.key); } if (event.type === gamejs.event.KEY_UP) { for (var key in that.keyconfig) { if (that.keyconfig.hasOwnProperty(key)) { if (event.key === gamejs.event[key]) { that.keyconfig.key = false; } } } system.log("KEYUP", event.key); } return keyconfig; }); }, eval_mouse: function () { /* in progress else if (event.type === gamejs.event.MOUSE_MOTION) { // if mouse is over display surface if (display.rect.collidePoint(event.pos)) { system.log("mousemove", testcoords); testcoords = event.pos; } } */ }, keyconfig: { K_UP: false, K_LEFT: false, K_RIGHT: false, K_DOWN: false } }; exports.eval_keys = input.eval_keys; </code></pre> <p>Output from Chrome's dev console:</p> <pre><code>Object {eval_keys: function} eval_keys: function () { arguments: null caller: null length: 0 name: "" prototype: Object __proto__: function Empty() {} &lt;function scope&gt; __proto__: Object </code></pre>
    singulars
    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.
 

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