Javascript 如何记录变量/数组/属性包含构造函数?
有时,我需要使用字符串或数值来决定构造函数之间的关系,这些构造函数共享同一个类 例如,现在我正在实现反向代理,它有几种代理管理模式:TCP、UDP和符号UDP 所以我有一个目标:Javascript 如何记录变量/数组/属性包含构造函数?,javascript,node.js,jsdoc,Javascript,Node.js,Jsdoc,有时,我需要使用字符串或数值来决定构造函数之间的关系,这些构造函数共享同一个类 例如,现在我正在实现反向代理,它有几种代理管理模式:TCP、UDP和符号UDP 所以我有一个目标: const servers = { TCP: ProxyControllerTCP, UDP: ProxyControllerUDP, UDP_sym: UDPProxyBidirectional }; 代理将根据先前的初始化请求选择适当的服务器模块 但是我如何记录服务器包含ProxyCont
const servers = {
TCP: ProxyControllerTCP,
UDP: ProxyControllerUDP,
UDP_sym: UDPProxyBidirectional
};
代理将根据先前的初始化请求选择适当的服务器模块
但是我如何记录
服务器
包含ProxyController
基类的构造函数呢?我需要它来与Visual Studio 2017 intellisense合作。答案包括两件事,这两件事都很难弄清楚
1.记录对象的值类型
这是记录对象包含哪些值的语法:
/** @type {[key: string]: number} **/
const myObjOfNumbers = {};
// Visual studio hints number type, even though the value is not defined right now
const num = myObjOfNumbers["hello world"];
2.定义构造函数类型
这更直观:
/** @type {new HTMLElement} **/
const test = null;
// Visual studio hints HTMLElement type, even though HTMLElement does not
// have a public constructor
const elm = new test();
你的力量加在一起:
我现在可以使用新服务器[“UDP”]
并获得基类的类型提示
/** @type {{[x:string]: new ProxyController}} **/
const servers = {
TCP: ProxyControllerTCP,
UDP: ProxyControllerUDP,
UDP_sym: UDPProxyBidirectional
};