【问题标题】:JSDoc for special singleton pattern用于特殊单例模式的 JSDoc
【发布时间】:2016-01-29 18:06:27
【问题描述】:

我有一个特殊的 JS 单例原型函数。

模式看起来或多或少类似于下面的示例。工作正常并完成工作,但遗憾的是,PhpStorm 对自动完成和其他有用的事情完全视而不见。

如何使用 JSDoc 告诉 IDE new Item 将导致使用 ItemPrototype 构建新对象,因此 new Item(1).getId() 将指向代码中的正确位置?

提前感谢您的宝贵时间。

var Item = (function(){
    var singletonCollection = {};

    var ItemPrototype = function(id){

        this.getId = function() {
            return id;
        };

        return this;
    };

    var Constructor = function(id){
        if (! (id in singletonCollection)) {
            singletonCollection[id] = new ItemPrototype(id);
        }

        return singletonCollection[id];
    };

    return Constructor;
})();

【问题讨论】:

  • 抱歉,您能更正您的代码 sn-p 吗?语法看起来有问题。
  • 好的,编辑了原帖。

标签: javascript phpstorm webstorm jsdoc


【解决方案1】:

您可以尝试以下方法:

/**
 * A description here
 * @class
 * @name Item
 */
var Item = (function(){
    var singletonCollection = {};

    var ItemPrototype = function(id){
        /**
         * method description
         * @name Item#getId
         */
        this.getId = function() {
            return id;
        };

        return this;
    };

    var Constructor = function(id){
        if (! (id in singletonCollection)) {
            singletonCollection[id] = new ItemPrototype(id);
        }

        return singletonCollection[id];
    };

    return Constructor;
})();

【讨论】:

  • 工作正常,但在每个方法中添加@name 会很烦人。但是检查了它,将 @name Item 添加到 ItemPrototype 文档就足够了。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多