简体   繁体   中英

Describe property defined with Object.defineProperty

I want to add JSDoc documentation for the property added as Object.defineProperty . I guess that something like this might work:

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

But the generated JSDoc explanation does not have this property:

$ 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" ]
    }
]

What will be the most appropriate approach to document this kind of properties? (Plugin maybe?)

This would do it:

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

I've used the @property {type} name syntax but never inside of a class. What I typically do is something like:

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

And then jsdoc uses this.foo to compute the name ( foo ) and which entity it belongs to. It appears that using @property {type} name in a class does not work.

If you specify the name with @name , then it knows what name to give to it and where it belongs. The # indicates that it is an instance variable (rather than static or inner). See the namepath documentation for details.

If you want to declare the member outside the class you can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM