Angular 扩展与 $provide.decorator
Angular extend vs $provide.decorator
我很困惑。你能解释一下 angular.extend() 和 $provide.decorator 之间的区别吗? Why and when use the second one?
decorator 做的事情与 extend 不同吗?搜索后找不到任何答案...
angular.extend()
is just yet another implementation of the standard JavaScript extend tool. You can find many other similar (when not identical) implementations, like Object.assign(), jQuery's, Underscore's...
$provide.decorator()
though is the Angular implementation of the decorator pattern,并且更强大,因为它允许您透明地更改提供者的行为,即无需修改依赖对象。文档中的示例非常简单:每次调用 $log.warn()
时,消息将自动添加前缀 'Decorated Warn: '
.
我很困惑。你能解释一下 angular.extend() 和 $provide.decorator 之间的区别吗? Why and when use the second one?
decorator 做的事情与 extend 不同吗?搜索后找不到任何答案...
angular.extend()
is just yet another implementation of the standard JavaScript extend tool. You can find many other similar (when not identical) implementations, like Object.assign(), jQuery's, Underscore's...
$provide.decorator()
though is the Angular implementation of the decorator pattern,并且更强大,因为它允许您透明地更改提供者的行为,即无需修改依赖对象。文档中的示例非常简单:每次调用 $log.warn()
时,消息将自动添加前缀 'Decorated Warn: '
.