Javascript 如何使用JSDoc(如lodash)对参数进行分组?
我想知道lodash如何根据用户输入的参数类型或数量,对其功能的参数建议进行分组,如下所示:Javascript 如何使用JSDoc(如lodash)对参数进行分组?,javascript,parameters,jsdoc,Javascript,Parameters,Jsdoc,我想知道lodash如何根据用户输入的参数类型或数量,对其功能的参数建议进行分组,如下所示: 在上图中,可以看到lodash中的每个函数都有8组建议。下面是我的示例代码: 函数示例: function filter(allowSize, max) { //do Something } /** * @param {object} allowSize * @param {number} allowSize.min * @param {number} allowSize.max
在上图中,可以看到lodash中的
每个函数都有8组建议。下面是我的示例代码:
函数示例:
function filter(allowSize, max) {
//do Something
}
/**
* @param {object} allowSize
* @param {number} allowSize.min
* @param {number} allowSize.max
*/
/**
* @param {number} allowSize
* @param {number} max
*/
function filter(allowSize: object): any;
function filter(min: number, max: number): any;
function filter(arg1: object | number, arg2?: number): any {
if (typeof arg1 === "object") {
// do something when the function only have 1 parameter
} else if (typeof arg1 === "number" && typeof arg2 === "number") {
// do something when the function has 2 parameters
} else {
// default
}
}
如果filter
函数只有一个参数,请使用下面的jsdoc:
function filter(allowSize, max) {
//do Something
}
/**
* @param {object} allowSize
* @param {number} allowSize.min
* @param {number} allowSize.max
*/
/**
* @param {number} allowSize
* @param {number} max
*/
function filter(allowSize: object): any;
function filter(min: number, max: number): any;
function filter(arg1: object | number, arg2?: number): any {
if (typeof arg1 === "object") {
// do something when the function only have 1 parameter
} else if (typeof arg1 === "number" && typeof arg2 === "number") {
// do something when the function has 2 parameters
} else {
// default
}
}
我想从建议中删除max
参数,但我不知道如何删除
去做
如果filter
函数有2个参数,请使用下面的jsdoc:
function filter(allowSize, max) {
//do Something
}
/**
* @param {object} allowSize
* @param {number} allowSize.min
* @param {number} allowSize.max
*/
/**
* @param {number} allowSize
* @param {number} max
*/
function filter(allowSize: object): any;
function filter(min: number, max: number): any;
function filter(arg1: object | number, arg2?: number): any {
if (typeof arg1 === "object") {
// do something when the function only have 1 parameter
} else if (typeof arg1 === "number" && typeof arg2 === "number") {
// do something when the function has 2 parameters
} else {
// default
}
}
我想在建议中将allowSize
参数重命名为min
,但我没有
知道怎么做吗
如何使用jsdoc执行上述操作?您可以使用函数重载方法,但javascript不支持此方法。那么如何执行lodash
答案是lodash使用typescript编写代码。typescript允许您使用重载方法编写函数。typescript将生成一个扩展名为.d.ts
的文件,其中包含与所编写代码相关的信息
示例:
function filter(allowSize, max) {
//do Something
}
/**
* @param {object} allowSize
* @param {number} allowSize.min
* @param {number} allowSize.max
*/
/**
* @param {number} allowSize
* @param {number} max
*/
function filter(allowSize: object): any;
function filter(min: number, max: number): any;
function filter(arg1: object | number, arg2?: number): any {
if (typeof arg1 === "object") {
// do something when the function only have 1 parameter
} else if (typeof arg1 === "number" && typeof arg2 === "number") {
// do something when the function has 2 parameters
} else {
// default
}
}
然后,使用命令tsc-d typescript\u file.TS编译TS文件
-d
参数用于告诉编译器创建声明文件