brainmassage
brainmassage

Reputation: 1254

Why does angular.bind() exists while core javascript has the same functionality?

In the angular documentation it says that you can take an object and bind it to a function with optional arguments like this:

angular.bind(self, fn, args);

If I understood correctly "self" becomes "this" for the returned (and modified) fn.

This is also easily done with core javascript:

fn.apply(obj, args);

Am I missing something here?

Upvotes: 1

Views: 58

Answers (1)

Estus Flask
Estus Flask

Reputation: 222310

angular.bind creates a wrapper function with specified context. It is a counterpart of Function.prototype.bind, not Function.prototype.apply.

angular.bind is there from old days when it was good manners for JS libraries to be self-sufficient and not rely on standards and polyfills (a tribute to jQuery and jQuery.proxy). This often resulted in smaller footprint.

angular.bind and fn.apply won't work with newed ES6 classes, it is preferable to stick to fn.bind if it fits the case.

Upvotes: 2

Related Questions