gpt4 book ai didi

javascript - 如何使用 JSDoc 记录方法所需的变量?

转载 作者:行者123 更新时间:2023-11-29 18:51:47 25 4
gpt4 key购买 nike

我应该这样做吗?

/**
* Constructor function
*
* @contructor
* @param {Number} x - a number.
*/
function foo(x){
this.x = x;
/**
* Sum to x.
* @param {Number} y - A number.
* @param {Object} this - The bar.
* @param {Number} this.x - A number [requered].
*/
this.bar(y) {
return this.x + y;
}
}

或者在这种情况下,我不应该在注释中定义 this.x 的必需定义。

最佳答案

在编写代码文档时,您不应向“外部”透露您的方法的内部结构。只需正确、正确地记录您的方法,每种方法是什么:

  • 接受(如果有的话)
  • 并返回(如果有的话)

...以简洁的形式。

但如果您想记录对象的内部结构,可以使用 @private@protected 标签。

JSDoc tags as "accessibility modifiers"


更像这样:

/**
* Constructs foo, that does something useful.
*
* @contructor
* @param {number} x - A number, which determines X.
*/
function foo(x){
/*
* An important number.
*
* @private
* @type {number}
*/
this._x = x;

/**
* Returns the summary of X and Y.
*
* @param {number} y - A number to add to X.
* @returns {number} The summary of X and Y.
*/
this.bar(y){
return this._x + y;
}
}

这样,当您使用类型检查器或 IDE 时,如果您忽略了必需的参数,您将收到通知。


JavaScript 文档选项:

对于类型化的 JavaScript,查看:


Usage of JSDoc

关于javascript - 如何使用 JSDoc 记录方法所需的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com