Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wholeheartedly agree that too many inline closure functions don't enhance readability. On the other hand I strongly dislike the <code>var self = this</code> style of writing closures, to which <code>this</code> is just a variant, as it's still too verbose in its declaration, and you introduce your own new 'keyword' whether that is <code>this</code> or <code>self</code>.</p> <p>What you want is to curry/bind the method.</p> <pre><code>function $A(o) { var a = []; for (var i = 0; i &lt; o.length; ++i) a.push(o[i]); return a; } Function.prototype.bind = function() { var _method = this; var a = $A(arguments) var o = a.shift(); return function() { return _method.apply(o, a.concat($A(arguments))); } } Function.prototype.curry = function() { var _method = this; var a = $A(arguments); return function() { return _method.apply(null, a.concat($A(arguments))); } } </code></pre> <p>The methods are taken from the <a href="http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework" rel="nofollow noreferrer">Prototype</a> library. I use these even in projects that don't use the Prototype library as they are very useful!</p> <p>In your case this means instead of writing:</p> <pre><code>var This = this; oRequest.onreadystatechange = function() { This.handleResponse(oRequest, sUsername) } </code></pre> <p>you can now write:</p> <pre><code>oRequest.onreadystatechange = this.handleResponse.curry(oRequest,sUsername); </code></pre> <p>However if you want to transfer the meaning of the this keyword you could do this</p> <pre><code>oRequest.onreadystatechange = this.handleResponse.bind(this,oRequest,sUsername); </code></pre> <p><code>handleResponse</code>, when called, will have the same <code>this</code> context as the <code>submitHandler</code>.</p>
    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.
    3. VO
      singulars
      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