admin管理员组文章数量:1327102
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;
}
本文标签: Javascript delegateStack Overflow
版权声明:本文标题:Javascript delegate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742204824a2432620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论