描述使用Object.defineProperty定义的属性

Ale*_*hov 2 javascript jsdoc jsdoc3

我想为添加的属性添加JSDoc文档Object.defineProperty.我猜这样的事情可能有用:

/** @constructor */                                                         
function MyClass() {                                                        
  /** @property {Number} rnd */                                             
  Object.defineProperty(this, 'rnd', {                                      
    get : function () { return Math.random(); }                             
  });                                                                       
}                                                                           
Run Code Online (Sandbox Code Playgroud)

但是生成的JSDoc解释没有这个属性:

$ jsdoc -X test.js
[
    {
        "comment": "/** @constructor */",
        "meta": {
            "range": [ 20, 167 ],
            "filename": "test.js",
            "lineno": 2,
            "path": "/jsdoctest",
            "code": {
                "id": "astnode100000001",
                "name": "MyClass",
                "type": "FunctionDeclaration",
                "paramnames": []
            },
            "vars": { "": null }
        },
        "kind": "class",
        "name": "MyClass",
        "longname": "MyClass",
        "scope": "global"
    },
    {
        "comment": "",
        "meta": {
            "range": [ 116, 159 ],
            "filename": "test.js",
            "lineno": 5,
            "path": "/jsdoctest",
            "code": {
                "id": "astnode100000012",
                "name": "get",
                "type": "FunctionExpression",
                "value": "function"
            }
        },
        "undocumented": true,
        "name": "get",
        "longname": "get",
        "kind": "function",
        "scope": "global"
    },
    {
        "kind": "package",
        "longname": "package:undefined",
        "files": [ "/jsdoctest/test.js" ]
    }
]
Run Code Online (Sandbox Code Playgroud)

记录此类属性的最合适方法是什么?(插件可能吗?)

Lou*_*uis 11

这样做:

/** @constructor */
function MyClass() {
  /**
   * @property {Number}
   * @name MyClass#rnd
   */
  Object.defineProperty(this, 'rnd', {
    get : function () { return Math.random(); }
  });
}
Run Code Online (Sandbox Code Playgroud)

我已经使用了@property {type} name语法但从未在类中使用过.我通常做的是:

/** @property {object} */
this.foo = {};
Run Code Online (Sandbox Code Playgroud)

然后jsdoc用于this.foo计算name(foo)及其所属的实体.似乎@property {type} name在课堂上使用不起作用.

如果您指定名称@name,则它知道要给它的名称和名称.的#表明它是一个实例变量(而不是静态的或内部).有关详细信息,请参阅namepath文档.