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?
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?
Share Improve this question edited Sep 29, 2011 at 10:22 AmGates 2,12316 silver badges29 bronze badges asked Sep 29, 2011 at 10:11 JMCamposJMCampos 6531 gold badge10 silver badges24 bronze badges 3-
1
FWIW, your code basically tries to emulate the ES5
.bind()
method. Have a look at the MDN documentation to see their implementation. – Felix Kling Commented Sep 29, 2011 at 10:24 - Wat output do you expect in "out" ? – AmGates Commented Sep 29, 2011 at 10:31
- Thanks Felix Kling. Gonna take a look at it. – JMCampos Commented Sep 29, 2011 at 10:42
1 Answer
Reset to default 6When 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;
}