JSDoc用于特殊的单例模式 [英] JSDoc for special singleton pattern

查看:84
本文介绍了JSDoc用于特殊的单例模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a special JS singleton prototype function.

模式看起来或多或少像下面的示例.干得好,干得好,但是遗憾的是,PhpStorm对于自动完成和其他有用的事情完全是盲目的.

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.

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

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?

提前感谢您的时间.

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

这篇关于JSDoc用于特殊的单例模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆