【问题标题】:Describe property defined with Object.defineProperty描述使用 Object.defineProperty 定义的属性
【发布时间】:2017-08-17 05:27:24
【问题描述】:

我想为添加为 Object.defineProperty 的属性添加 JSDoc 文档。我想这样的事情可能会起作用:

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

但是生成的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" ]
    }
]

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

【问题讨论】:

  • 不是运行时的问题,问题是关于JSDoc如何解析JavaScript文件。

标签: javascript jsdoc jsdoc3


【解决方案1】:

这样就可以了:

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

我使用过@property {type} name 语法,但从未在类中使用过。我通常做的是这样的:

/** @property {object} */
this.foo = {};

然后 jsdoc 使用this.foo 来计算名称(foo)以及它属于哪个实体。在课堂上使用@property {type} name 似乎不起作用。

如果您使用@name 指定名称,那么它就知道要给它取什么名称以及它所属的位置。 # 表示它是一个实例变量(而不是静态变量或内部变量)。详情请见namepath documentation。

【讨论】:

    【解决方案2】:

    如果你想在类之外声明成员,你可以使用:

    /**
     * @constructor
     */
    function MyClass() {};
    
    /**
     * Get the number of differed values in this map
     *
     * @member {number} count
     * @memberOf module:moduleName.MyClass#
     * @readonly
     */
    Object.defineProperty(MyClass.prototype, 'rnd', {
      get : function () { return Math.random(); }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2022-06-10
      相关资源
      最近更新 更多