Javascript'this'覆盖Z组合器和所有其他递归函数 [英] Javascript 'this' overwriting in Z combinator and every other recursive function

查看:29
本文介绍了Javascript'this'覆盖Z组合器和所有其他递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一个由 Z-combinator 实现的递归函数,如图所示此处这里所以它不使用arguments.callee,因为它将在即将到来的ES6中被弃用.

I have a recursive function implemented by a Z-combinator as is shown here and here so it makes no use of arguments.callee since it will be deprecated in upcoming ES6.

问题

Z-combinator 和我目前看到的所有递归匿名函数的主要问题是它们将 de this 值更新到内部函数作用域(在 return 子句处自返回),所以引用顶层的 this 丢失了,我想通过所有内部函数来维护它.

The main issue with the Z-combinator and all recursive anonymous functions I've seen so far is that they updates de this value to the inner function scope (the self-returned at the return clause), so the this that references the top level is lost, and I want to maintain it through all the inner functions.

有没有办法保持顶级 this 而不将它作为额外的函数参数传递,这是摆脱这个问题的最明显的方法,但没有我想要的那么干净?

Is there a way to maintain the top level this 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?

现在我通过将顶部 this 引用传递给 Z-combinator 来解决这个问题,如下所示:

Right now I solve the issue by passing the top this reference to the Z-combinator like this:

Co.Utilities.Z(this.createHTMLFromLOM)(this.LOM, this);

在递归函数中,我通过像这样传递顶部 this 值来返回相同的函数:

in the recursive function I return the same function by passing the top this value like this:

function createHTMLFromLOM(callee:any, LOM_section:LOM, self:any):void {
    /* Some other code. */
    return callee(LOM_section.children[widget], self);
}

这是我的Z-combinator定义:

function Z(func:any):any {
        var f = function () {
            return func.apply(null, [f].concat([].slice.apply(arguments)));
        };
         return f;
    }

谢谢

推荐答案

您可以执行以下操作:

var me = this;

并将 me 作为参数传递给 Z Combinator.

and the pass me as an argument to the Z Combinator.

这篇关于Javascript'this'覆盖Z组合器和所有其他递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆