簡體   English   中英

描述使用Object.defineProperty定義的屬性

[英]Describe property defined with Object.defineProperty

我想為添加為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" ]
    }
]

記錄此類屬性的最合適方法是什么? (插件可能嗎?)

這樣做:

/** @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文檔

如果要在類外聲明成員,可以使用:

/**
 * @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(); }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM