【发布时间】:2011-09-29 10:11:00
【问题描述】:
我有以下代码
function createDelegate(object, method)
{
var shim = function()
{
method.apply(object, arguments);
}
return shim;
}
this.test = 3;
var pAction = {to: this.test}
this.tmp = createDelegate(this, function()
{
print("in: " + pAction.to);
return pAction.to;
});
print("out: " + this.tmp());
但由于某种原因,我得到以下结果
in: 3
out: undefined
有人知道这是什么原因吗?
【问题讨论】:
-
FWIW,您的代码基本上试图模拟 ES5
.bind()方法。 Have a look at the MDN documentation 看看他们的实现。 -
Wat 输出你期望在“out”中吗?
-
谢谢菲利克斯克林。去看看吧。
标签: javascript delegates