【问题标题】:Dojo 1.6 hitch() scope of "this" within require statementDojo 1.6 hitch() 要求语句中“this”的范围
【发布时间】:2012-12-25 04:07:06
【问题描述】:

我的 dojo 应用程序出现了一个奇怪的问题。它只是关于范围和连接对象本身。以下代码

function(data) {
        console.info(this); // [I]
        var fun = require(["dojo/Deferred"], function(Deferred) {
            console.info(this); // [II]
        });

        lang.hitch(this, fun());
    }

创建这个输出

Object{...} // from [I]
Window index.php [II]

所需的输出必须是对象的 2 倍。我以为我理解了挂接机制,但是通过调用 lang.hitch(this, fun());在我看来,“this”是由 [I] 打印的对象。

希望大家帮忙!

提前致谢!

【问题讨论】:

    标签: javascript dojo scope require execution


    【解决方案1】:

    你要做的就是限制回调函数的范围:

    function(data) {
            console.info(this); // [I]
            var fun = require(["dojo/Deferred"], lang.hitch(this,function(Deferred) {
                console.info(this); // [II]
            }));
    }
    

    而不是在被求值lang.hitch(this, fun());lang.hitch(this, fun());

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 2014-01-07
      • 2020-11-09
      相关资源
      最近更新 更多