gpt4 book ai didi

javascript - 什么是 "decorators"以及它们是如何使用的?

转载 作者:IT老高 更新时间:2023-10-28 13:18:35 24 4
gpt4 key购买 nike

我很好奇 AngularJS 中的装饰器到底是什么。除了 AngularJS documentation 中的简介外,没有太多关于装饰者的在线信息。并在 youtube video 中简短(尽管很有趣)提及.

正如 Angular 人所说,装饰器是:

Decoration of service, allows the decorator to intercept the service instance creation. The returned instance may be the original instance, or a new instance which delegates to the original instance.

我真的不知道是什么意思,而且我不确定你为什么要将这个逻辑与服务本身分开。例如,如果我想在不同条件下返回不同的东西,我只需将不同的参数传递给相关函数或使用另一个共享该私有(private)状态的函数。

我还是个 AngularJS 菜鸟,所以我确定这只是我的无知和/或坏习惯。

最佳答案

$provide.decorator 的一个很好的用例是当您需要对您的模块所依赖的某些第三方/上游服务进行较小的“调整”,同时保持服务完好无损(因为您不是服务的所有者/维护者)。 Here是 plunkr 上的一个演示。

关于javascript - 什么是 "decorators"以及它们是如何使用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16075982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com