Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript 'this' overwriting in Z combinator and every other recursive function
    primarykey
    data
    text
    <p><strong>Background:</strong></p> <p>I have a recursive function implemented by a <strong>Z-combinator</strong> as is shown <a href="https://stackoverflow.com/questions/17645356/anonymous-recursion-any-way-to-replace-javascript-arguments-callee-to-other">here</a> and <a href="http://am.aurlien.net/post/2810658101/z-combinator" rel="nofollow noreferrer">here</a> so it makes no use of <code>arguments.callee</code> since it will be deprecated in upcoming <strong>ES6</strong>.</p> <p><strong>Issue</strong></p> <p>The main issue with the <strong>Z-combinator</strong> and all recursive anonymous functions I've seen so far is that they updates de <code>this</code> value to the inner function scope (the self-returned at the <code>return</code> clause), so the <code>this</code> that references the top level is lost, and I want to maintain it through all the inner functions.</p> <p>Is there a way to maintain the top level <code>this</code> without passing it as an additional function argument, which is the most obvious way to get rid of this issue but is not as clean as I want?</p> <p><strong>EDIT:</strong></p> <p>Right now I solve the issue by passing the top <code>this</code> reference to the <strong>Z-combinator</strong> like this:</p> <pre><code>Co.Utilities.Z(this.createHTMLFromLOM)(this.LOM, this); </code></pre> <p>in the recursive function I return the same function by passing the top this value like this:</p> <pre><code>function createHTMLFromLOM(callee:any, LOM_section:LOM, self:any):void { /* Some other code. */ return callee(LOM_section.children[widget], self); } </code></pre> <p>This is my <strong>Z-combinator</strong> definition:</p> <pre><code>function Z(func:any):any { var f = function () { return func.apply(null, [f].concat([].slice.apply(arguments))); }; return f; } </code></pre> <p>Thanks</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.
 

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