简体   繁体   中英

typescript constructor new object

What is the meaning of the second parameter in the constructor:

(from this example )

constructor(private url: string, private WebSocketCtor: { new(url:string): WebSocket } = WebSocket) {}

Especially the = WebSocket part at the end. Why do I need this? In the example above it is called like this:

bootstrap(AimApp, [
  ...,
  provide(RxWebSocket, {useFactory: (url:string) => {
    return new RxWebSocket(url, WebSocket);
  }, deps: [SOCKET_URL]})
]);

Since I am not very familiar with Angular, how would you call it otherwise?

{ new(url:string): WebSocket } this defines a constructor signature, which is used to hold class constructors. = WebSocket provides a default value for the parameters, the WebSocket class.

The point of this is to allow the user to plug in a custom WebSocket class, which is compatible with the WebSocket class, but also provides a default implementation by providing a default value for the WebSocketCtor parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM