Javascript委托

JMC*_*pos 2 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());
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我得到以下结果

in: 3
out: undefined
Run Code Online (Sandbox Code Playgroud)

有人知道这个的原因吗?

mck*_*k89 6

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

function createDelegate(object, method)
{
 var shim =  function()
 {                  
    return method.apply(object, arguments);
 }
 return shim;
}
Run Code Online (Sandbox Code Playgroud)