如何使用混合参数类型记录JSDoc? [英] How do you document JSDoc with mixed parameter type?

查看:282
本文介绍了如何使用混合参数类型记录JSDoc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可以混合参数类型,如何使用 JSDoc 在JavaScript中记录方法?

How do I document a method in JavaScript using JSDoc when the parameter type can be mixed?

我在Dialog对象上有方法我可以在哪里显示HTML或我自己的Viewable对象。方法JSDoc如下所示:

I have method on a Dialog object where I can show HTML or my own Viewable objects. The method JSDoc looks like this:

/**
 * Can pass in viewable object, or some HTML element
 *
 * @param viewable viewable {Viewable} or HTML element {HTMLElement} or String {string}
 * @param {Boolean} cancelable is cancellable
 * @param title string or data object of String and Id {Title:String, Id:String} for setting HTML id value
 * @param {Array} actions array of functions actions display buttons on the bottom connecting to the passed in functions
 * @param {String} size mode. Can be mini,small,medium,large,maxi. Or of type {width:number, height:number}
 * @param {Number} zindex starting z-order. Note: first level dialog = 10,11,12, second level dialog 13,14,15 etc.
 */
Dialog.showElement = function(viewable, cancelable, title, actions, mode, zindex){
..
}

因为JS不允许方法重载,我需要创建这些类型的方法,其中方法中的参数可以是两种不同的类型。有没有办法在JSDoc中记录这个,或者JSDoc只允许你用一种类型记录一个param?

Because JS doesn't allow method overloading, I need to create these types of methods, where a parameter in a method can be two disparate types. Is there a way to document this in JSDoc, or can JSDoc only let you document a param with one type?

另外你如何记录<$ c类型的参数$ c> {标题:字符串,Id:字符串} ?也就是说,传入的对象不属于某种类型。准,JSON对象。

Also how would you document a paramater of type {Title:String, Id:String}? That is, an object passed in that is not of a type. Quasi, a JSON object.

推荐答案

您可以使用 | 分隔符在方法类型签名中指定多个类型:

You can use the | separator to specify multiple types in the method type signature:

/**
 * Some method
 * @param {Object|string|number} param The parameter.
 * @returns {Object|string|number} The modified param.
 */
function doSomething(param) {
    return etc..
};

这篇关于如何使用混合参数类型记录JSDoc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆