Javascript委托

时间:2011-09-29 10:11:00

标签: javascript delegates

我有以下代码

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

有人知道这个的原因吗?

1 个答案:

答案 0 :(得分:6)

创建委派函数时,必须返回旧函数的结果:

function createDelegate(object, method)
{
 var shim =  function()
 {                  
    return method.apply(object, arguments);
 }
 return shim;
}