• 0

Implement the bind function in javascript

Implement the Function.prototype.bind function in javascript

  • bind allows you to change the scope of this
  • also allows you to create functions when called are prefixed with arguments as shown in the solution (aka currying, partially applied functions)

Understanding Closures, Currying, Partially applied functions in javascript is pre-requisite before seeing the solution. I would recommend reading that article first.

The meat of the solutions is this

Function.prototype.bind = function(scope) {
    var fn = this
    return function() {
      return fn.apply(scope, args)

As bind also has a use case when you want to use currying and partially applied functions, the fully featured implementation looks like the following: