gpt4 book ai didi

Javascript委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 16:24:28 28 4
gpt4 key购买 nike

我有以下代码

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

有人知道这是什么原因吗?

最佳答案

当您创建委托(delegate)函数时,您必须返回旧函数的结果:

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

关于Javascript委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7595768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com