javascript - 特殊单例模式的 JSDoc

标签 javascript phpstorm webstorm jsdoc

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

模式看起来或多或少类似于下面的示例。工作正常并完成工作,但遗憾的是 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;
})();

最佳答案

您可以尝试以下操作:

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

关于javascript - 特殊单例模式的 JSDoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416937/

相关文章:

javascript - JavaScript 中的正则表达式使用 [\w.]+ 而不是 .+

javascript - JQuery 在 Jest 测试中检查元素可见性

javascript - 使用本地计算机上的 js/html 文件读取本地文本文件

PhpStorm - 关于 NULL 值的检查

javascript - 当我在 WebStorm 中运行 JavaScript 文件时,显示无法在模块外部导入语句

javascript - 使用传递给 Rust 的 JavaScript 对象时是否会影响性能?

intellij-idea - JetBrains Idea 自定义连字

java - 如何在 phpstorm(基于速度)文件模板中将字符串转换为驼峰式大小写?

intellij-idea - Intellij:左右移动标签

ecmascript-6 - 如何在WebStorm中启用ES6 +语法?