函数(){}.bind(this)和角的区别。绑定(这个函数(){ })

时间:2022-02-07 20:06:07

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 个解决方案

#1


2  

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.

内置Function.prototype。绑定函数在旧的浏览器中不存在,比如ie8。但是,使用polyfill可以实现相同的语法。这就是角在内部的作用。

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.

角。绑定函数不使用function .prototype。绑定,所以在旧的浏览器中使用它是可能的。当然,如果您使用的是不积极支持旧浏览器的角度版本,那么这一点毫无意义。

#1


2  

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.

内置Function.prototype。绑定函数在旧的浏览器中不存在,比如ie8。但是,使用polyfill可以实现相同的语法。这就是角在内部的作用。

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.

角。绑定函数不使用function .prototype。绑定,所以在旧的浏览器中使用它是可能的。当然,如果您使用的是不积极支持旧浏览器的角度版本,那么这一点毫无意义。