JS function returning another function

Posted by Michael on Stack Overflow See other posts from Stack Overflow or by Michael
Published on 2010-04-28T13:38:46Z Indexed on 2010/04/28 13:43 UTC
Read the original article Hit count: 281

Filed under:
|
|

I want to understand about variables, that has been used in returning function. This is example code

Prototype = {}

Prototype.F =
{
  bind: function()
  {
    var args = arguments, __method = args.shift(), object = args.shift();
    return function()
    {
        return __method.apply(object, args.concat(arguments));
    }
  }
}

function ObjectA()
{
    ...
    this.addListener = Prototype.F.bind(this.eventSource.addListener,
        this.eventSource);
    ...
}


var a = ObjectA();
a.addListener(this); // assuming 'this' here will point to some window object

As I understand the returning function in F() is not evaluated until it's called in the last line. It's ok to accept. So addListener will hold a function body containing 'apply'.

But what I don't understand, when addListener is called, what kind of parameters it is going to have? particularly _method and args will always be uninitialized?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about function