JMCampos
JMCampos

Reputation: 653

Javascript delegate

I have the following the code

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());

But for some reason I get the following result

in: 3
out: undefined

Anyone knows the reason for this?

Upvotes: 2

Views: 8399

Answers (1)

mck89
mck89

Reputation: 19241

When you create the delegated function you must return the result of the old function:

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

Upvotes: 6

Related Questions