2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.