Home > Programming > JavaScript magic

JavaScript magic

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

If you want to spent an hour studying how this one function in JavaScript works there is an excellent demonstration at John Resig’s blog.

The code uses three types of method invocation in just a couple of lines (shift(), apply and call). Isn’t JavaScript too complex?

Categories: Programming Tags:
  1. No comments yet.