最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript delegate - Stack Overflow

programmeradmin7浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 6

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;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论