rkmax
rkmax

Reputation: 18125

Difference between function(){}.bind(this) and angular.bind(this, function() {})

What is the difference between the following codes

function Ctrl($scope) {
    $scope.$watch(function() {
        return this.variable;
    }.bind(this), /*...*/);
}

and

function Ctrl($scope) {
    $scope.$watch(angular.bind(this, function() {
        return this.variable;
    }, /*...*/);
}

for me are the same but is there any advantage of using angular.bind?

Upvotes: 2

Views: 825

Answers (1)

Taylor Buchanan
Taylor Buchanan

Reputation: 4756

The built-in Function.prototype.bind function does not exist in older browsers, such as IE 8. However, the same syntax can be achieved by using a polyfill. This is essentially what Angular is doing internally.

The angular.bind function does not use Function.prototype.bind, so it may be possible to use it in older browsers. Of course, this point is moot if you are using a version of Angular that does not actively support those older browsers.

Upvotes: 3

Related Questions