我的 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 倍。我以为我理解 Hook 机制,但是通过调用 lang.hitch(this, fun());在我看来,“this”是 [I] 打印的对象。
希望大家帮帮忙!
提前致谢!
最佳答案
您要做的是限制回调函数的范围:
function(data) {
console.info(this); // [I]
var fun = require(["dojo/Deferred"], lang.hitch(this,function(Deferred) {
console.info(this); // [II]
}));
}
而不是在求值后挂到 require 函数的返回结果 lang.hitch(this, fun());
关于javascript - Dojo 1.6 hitch() "this"范围在 require 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263533/