函数(){}.bind(this) 和 angular.bind(this, function() {}) 之间的区别
Difference between function(){}.bind(this) and angular.bind(this, function() {})
以下代码有什么区别
function Ctrl($scope) {
$scope.$watch(function() {
return this.variable;
}.bind(this), /*...*/);
}
和
function Ctrl($scope) {
$scope.$watch(angular.bind(this, function() {
return this.variable;
}, /*...*/);
}
对我来说是一样的,但是使用angular.bind
有什么好处吗?
内置的
Function.prototype.bind
函数在较旧的浏览器中不存在,例如IE 8。但是,使用填充代码可以实现相同的语法。这本质上是Angular在内部所做的。
angular.bind
函数不使用 Function.prototype.bind
,因此可以在较旧的浏览器中使用它。当然,如果您使用的是不主动支持那些旧浏览器的 Angular 版本,这一点是没有意义的。
相关文章:
- “this.foo”总是与“foo.bind(this)”相同吗?
- jQuery off() and bind(this)
- 如何使用.bind(this)删除对象的事件侦听器
- 函数(){}.bind(this) 和 angular.bind(this, function() {}) 之间的区别
- ESLint 和 This-Bind 运算符
- 如何在附加了 .bind(this) 的回调上删除事件侦听器
- 使用bind(this)并一起传递参数
- .bind(this)vs var context=this在Javascript中-what'It’这是最好的
- Javascript bind "this" to event
- ES6中的.on('error', this. onerror .bind(this))的替代方案
- 如何理解“{ fun: function.bind(this) }”
- 为什么调用.bind(this)时函数中没有定义?
- 深刻理解:为什么.bind(this)在与new Promise一起使用时似乎不遵循正常的规则?
- Bind (this)在ajax成功函数上不起作用
- 为什么.bind(this)修复了setState渲染问题?
- 我如何在this.function.bind(this)中替换任何数值作为参数
- this. somefunction .bind(this)是否多余?
- 何时在knockoutjs原型中使用.bind(this)
- AngularJS using bind(this)
- 使用&;self = this&;bind(this)用于内部函数