explanation about prototype.js function binding code

Posted by resopollution on Stack Overflow See other posts from Stack Overflow or by resopollution
Published on 2010-04-21T09:45:59Z Indexed on 2010/04/21 10:53 UTC
Read the original article Hit count: 283

From: http://ejohn.org/apps/learn/#2

Function.prototype.bind = function(){
  var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
  return function(){
    return fn.apply(object,
      args.concat(Array.prototype.slice.call(arguments)));
  };
};

Can anyone tell me why the second return is necessary (before fn.apply)?

Also, can anyone explain why args.concat is necessary? Why wouldn't it be re-written as:

fn.apply(object, args)

instead of

return fn.apply(object,
          args.concat(Array.prototype.slice.call(arguments)));

© Stack Overflow or respective owner

Related posts about prototypejs

Related posts about prototype