用于特殊单例模式的JSDoc

JSDoc for special singleton pattern

本文关键字:JSDoc 单例模式 用于      更新时间:2023-09-26

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

图案看起来或多或少像下面的例子。好好工作,做好工作,但遗憾的是,PhpStorm对自动完成和其他有用的事情完全视而不见。

如何使用JSDoc告诉IDE,新的Item将导致使用ItemPrototype构建的新对象的结束,因此新的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;
})();

您可以尝试以下操作:

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