Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem here is not <em>exactly</em> a closure or scoping problem. The problem is that when you assign <code>this.readSuccess</code> to a variable, you assign the function itself without any notion of the object it originaly belongs to.</p> <p>In the same way, you can take a regular, "stand-alone" function and use it as method of an object:</p> <pre><code>function hello() { alert("Hello "+this.planet); } var planet = "Earth"; hello(); // -&gt; 'Hello Earth' var Venus = { planet: "Venus" }; hello.apply(Venus); // -&gt; 'Hello Venus' Venus.hello = hello; Venus.hello(); // -&gt; 'Hello Venus' </code></pre> <p>And your problem can be replicated in this example</p> <pre><code>var helloVenus = Venus.hello; helloVenus(); // -&gt; 'Hello Earth' </code></pre> <p>So your problem is to assign this.readSuccess to some variable <strong><em>and having it called as a method of this</em></strong>. Which can be done with a closure as demonstrated by Pointy. Since I don't know what "SomeAjaxCall" actually does, it's hard to know if the value of <code>this</code> is actually lost and if <code>var obj = this</code> is actually needed. Chances are that it's not, so you can be fine with this kind of code:</p> <pre><code>var helloVenus = function() { Venus.hello() } helloVenus(); // -&gt; 'Hello Venus' </code></pre> <p>In your case, that would be (<strong>edit:</strong> adding the arguments passed to the handler) :</p> <pre><code>SomeObject.prototype.refreshData = function() { var read_obj = new SomeAjaxCall( "read_some_data", { }, function () { this.readSuccess.apply(this, arguments) }, function () { this.readFail.apply(this, arguments) } ); } </code></pre> <p>As noted previously, several js frameworks offer a <code>bind</code> function to simplify this kind of issue. But you don't need a complete framework just for this : here is a perfectly fine <code>Function#bind</code> method that works an plain javascript:</p> <pre><code>Function.prototype.bind = function(obj) { var __method = this; var args = []; for(var i=1; i&lt;arguments.length; i++) args.push(arguments[i]); return function() { var args2 = []; for(var i=0; i&lt;arguments.length; i++) args2.push(arguments[i]); return __method.apply(obj, args.concat(args2)); }; } </code></pre> <p>With the help of <code>Function#bind</code>, you can write:</p> <pre><code>SomeObject.prototype.refreshData = function() { var read_obj = new SomeAjaxCall( "read_some_data", { }, this.readSuccess.bind(this), this.readFail.bind(this) ); } </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.
    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