最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I correctly document instance members added via Object.defineProperties? - Stack Overflow

programmeradmin1浏览0评论

I have a class which defines a few instance properties via Object.defineProperties and I'm having great difficulty getting JSDoc 3 to recognize that they belong to their class.

Here's a simplified version of what I'm working with:

/** @exports mymodule */
function mymodule(exports) {
    /** @constructor
      * @param {String} foo A foo.
      * @param {String} bar A bar.
      * @classdesc Has a foo and a bar.
      */
    function Example(foo, bar) {
        Object.defineProperties(this, {
            /** A foo and a bar
              * @memberof Example
              */
            foobar: { enumerable: false, value: foo + bar, writable: false }
        });
    }

    exports.Example = Example;
}

When I run JSDoc, I get output for mymodule, Example, foo, and bar, but not foobar. If I remove the @memberof tag for foobar, it get registered as a global. I've tried @memberof mymmodule~Example, adding @lends to both the Object.defineProperties call and the object passed to it, and converting it to Object.defineProperty, but the results don't change.

How can I document foobar as belonging to Example?

I have a class which defines a few instance properties via Object.defineProperties and I'm having great difficulty getting JSDoc 3 to recognize that they belong to their class.

Here's a simplified version of what I'm working with:

/** @exports mymodule */
function mymodule(exports) {
    /** @constructor
      * @param {String} foo A foo.
      * @param {String} bar A bar.
      * @classdesc Has a foo and a bar.
      */
    function Example(foo, bar) {
        Object.defineProperties(this, {
            /** A foo and a bar
              * @memberof Example
              */
            foobar: { enumerable: false, value: foo + bar, writable: false }
        });
    }

    exports.Example = Example;
}

When I run JSDoc, I get output for mymodule, Example, foo, and bar, but not foobar. If I remove the @memberof tag for foobar, it get registered as a global. I've tried @memberof mymmodule~Example, adding @lends to both the Object.defineProperties call and the object passed to it, and converting it to Object.defineProperty, but the results don't change.

How can I document foobar as belonging to Example?

Share edited Sep 18, 2012 at 15:20 Ben Blank asked Sep 17, 2012 at 5:43 Ben BlankBen Blank 56.6k28 gold badges132 silver badges163 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

After digging through every example I could find, I finally assembled the necessary incantation — @memberof is indeed the trick, but JSDoc seems to require that modules being used in namepaths be explicitly marked as such. The following worked perfectly:

/** A foo and a bar
  *
  * @type String
  * @instance
  * @memberof module:mymodule~Example
  */

You could also try the @lends annotation in place of @memberOf like this :

Object.defineProperties(this, /** @lends Example# */{
    /** A foo and a bar */
    foobar: { enumerable: false, value: foo + bar, writable: false }
});

Don't forget the sharp symbol after the class name to tell jsdoc members are instance members and not static members.

After fiddling around for many hours, I finally got it to work with @memberOf!. Note the uppercase "O" and the bang "!"

/**
 * Description
 * @memberOf! MyClass
 * @type {String}
 */
var myString;

In case you want to use defineProperty instead, a @type annotation is sufficient (at least for IntelliJ to infer the proper type for the property):

/** @type SomeType */
Object.defineProperty(this, "someProperty", {
    get: () => new SomeType()
});
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>