alphagov/govuk_frontend_toolkit — pull request #120 by edds (2014-07-25 08:38:28+00:00)
Title: Add polyfill for Function.prototype.bind

So that we can easily bind a `this` to function calls in event handlers.

---

We are currently writing lots of JavaScript using a style like:

```javascript
$el.click($.proxy(this.clickHandler, this));
```

I am proposing we switch to using the new `Function.prototype.bind` syntax so that same call would look like:

```javascript
$el.click(this.clickHandler.bind(this));
```

Bind is supported natively in modern browsers and is the way that binding a `this` will be done in the future. It is also slightly cleaner syntax.

The reasons not to do this is extending native prototypes is often seen as bad practice in JavaScript. It will effect what happens when you try and do a `for(attribute in object)` as the methods we have added to the prototype will also be listed. However, the number of times you will try and do a loop like that on a function is very small and in that case we should be either adding a check for `hasOwnProperty` or using a jQuery iterator.

Having the polyfill in the toolkit means we wouldn't need to add it to each project individually.