JavaScript ES6 中的静态方法和 Angular 2 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41861092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static Methods and Angular 2 Services in JavaScript ES6
提问by Ore
While coding an app with Angular 2 and multiple calculation services I faced the following questions:
在使用 Angular 2 和多个计算服务编写应用程序时,我遇到了以下问题:
- When do I use static in a Angular service provided on application level? Is that nonsense?
- How does a static method reflect on performance? Lets say a couple hundret objects call at the same time the same static method. Is this method instantiated more than once?
- 我什么时候在应用程序级别提供的 Angular 服务中使用静态?这是胡说八道吗?
- 静态方法如何反映性能?假设有几个 hundret 对象同时调用相同的静态方法。这个方法实例化不止一次吗?
This is a snap of the class, that provides me multiple calculation methods and is instantiated on application level:
这是该类的一个快照,它为我提供了多种计算方法并在应用程序级别进行了实例化:
@Injectable()
export class FairnessService {
constructor(){}
private static calculateProcentValue(value: number, from: number): number {
return (Math.abs(value) / Math.abs(from)) * 100;
}
public static calculateAllocationWorth(allocation: Allocation): number {
...
}
}
Thanks for helping.
谢谢你的帮助。
回答by Seid Mehmedovic
1) Staticmethods of a class, unlike instancemethods, belong to (are visible on) the class itself (not an instance of it). They do not depend on the instance members of a class and will usually take input from the parameters, perform actions on it, and return some result. They act independently.
1)类的静态方法,与实例方法不同,属于(可见)类本身(不是它的实例)。它们不依赖于类的实例成员,通常会从参数中获取输入,对其执行操作,并返回一些结果。他们独立行动。
They do make sense in Angular services. There are situations where we don't actually need to use an instance of the service, and we don't want to make a new dependency on it, we only need the access to the methods our service carries. Here staticmembers come in.
它们在 Angular 服务中确实有意义。在某些情况下,我们实际上并不需要使用服务的实例,也不想对其产生新的依赖,我们只需要访问我们的服务所携带的方法。这里静态成员进来了。
The example of using the static method defined in the service:
使用服务中定义的静态方法的示例:
import { FairnessService } from './fairness.service';
export class MyComponent {
constructor() {
// This is just an example of accessing the static members of a class.
// Note we didn't inject the service, nor manually instantiate it like: let a = new A();
let value = FairnessService.calculatePercentValue(5, 50);
let value2 = FairnessService.calculatePercentValue(2, 80);
console.log(value); // => 10
console.log(value2); // => 2.5
}
}
2) Staticmethods have no impact on the performance. As we've ascertained above, they do not depend on any instance of the class, and invoking those methods will in no way instantiate the class.
2)静态方法对性能没有影响。正如我们在上面确定的那样,它们不依赖于类的任何实例,并且调用这些方法绝不会实例化类。
For more information, it's explained well on: http://www.typescriptlang.org/docs/handbook/classes.html
有关更多信息,它的解释很好:http: //www.typescriptlang.org/docs/handbook/classes.html
回答by chrispy
Static methods are represented as global variables (I think?) in the Angular application, so I think they would only be instantiated once each. Therefore I think it would not have a large impact on performance (relative to an instantiation of the class for each component that needs it.)
静态方法在 Angular 应用程序中表示为全局变量(我认为?),所以我认为它们每个只会被实例化一次。因此,我认为它不会对性能产生很大影响(相对于需要它的每个组件的类的实例化。)
I use static when I don't want to inject the service and get an instance just to leverage context-agnostic formatting/utility methods. Application-wide versions of these don't seem unreasonable to me.
当我不想注入服务并获取实例只是为了利用与上下文无关的格式/实用程序方法时,我使用静态。这些应用程序范围的版本对我来说似乎并不合理。

