JSDoc for special singleton pattern

旧城冷巷雨未停 提交于 2019-12-10 22:19:44

问题


I have a special JS singleton prototype function.

Pattern looks more or less like example below. Work fine and do the job, but sadly PhpStorm is totally blind regarding to auto-completion and other useful things.

How to tell the IDE using JSDoc that new Item will result in the end in new object build with ItemPrototype, so new Item(1).getId() will point to the right place in the code?

Thanks in advance for your time.

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

回答1:


You can try the following:

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


来源:https://stackoverflow.com/questions/33416937/jsdoc-for-special-singleton-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!