简体   繁体   English

JsDoc 和 WebStorm:参考导出

[英]JsDoc and WebStorm: Refer to exports

I have a module like this:我有一个这样的模块:

/**
 * Do something with target. A bunch of these methods
 * @param target
 */
function doJob(target) {
    target.something = 'value';
}

module.exports = {
    doJob
};

WebStorm correctly recognizes that doJob is exported by this module, and gives nice intellisense. WebStorm 正确识别此模块导出的doJob ,并提供了很好的智能感知。

Now I want to add facility so that I can bind all exported methods with a target.现在我想添加工具,以便我可以将所有导出的方法与目标绑定。

module.exports = {
    doJob
};

const oldExports = Object.assign({}, module.exports);
module.exports.bind = function (target) {
    const newExports = {};
    for (var key in oldExports) {
        newExports[key] = oldExports[key].bind(null, target);
    }
    return newExports;
};

The idea is that consumers can get normal exports and use functions in a procedural style, or they can get the same set of functions, bound to their local target .这个想法是消费者可以获得正常的导出并以程序风格使用函数,或者他们可以获得相同的函数集,绑定到他们的本地target

My problem is now, how do I tell WebStorm (using JsDoc) that the return value of bind() function is the same thing as module.exports ?我现在的问题是,我如何告诉 WebStorm(使用 JsDoc) bind()函数的返回值bind() module.exports相同?

Things I've tried:我尝试过的事情:

  • @returns {module.exports}
  • @returns {exports}
  • Declare @module at top, then @returns that symbol在顶部声明@module ,然后@returns该符号
  • @alias - this unfortunately seem to completely take over the symbol, so now I no longer have anything at module.exports @alias - 不幸的是,这似乎完全接管了符号,所以现在我在module.exports没有任何东西了

I realize this is not the end of the world.我意识到这不是世界末日。 But I feel like I am sooooo close to getting everything to fit together, it's driving me crazy.但我觉得我非常接近让所有东西组合在一起,这让我发疯。

Any idea what's the proper way to do this in JsDoc, with the added benefit that WebStorm / PhpStorm will know how to work with it?知道在 JsDoc 中执行此操作的正确方法是什么,还有 WebStorm / PhpStorm 知道如何使用它的额外好处?

Ok, I think I got it.好的,我想我明白了。

function doJob(target) {
    target.something = 'value';
}

class JobsModule {
    constructor() {
        Object.assign(this, /** @lends {JobsModule.prototype} */ {
            doJob,
            // other exports go here
        });
    }
}

module.exports = new JobsModule();

/**
 * @return {JobsModule}
 */
module.exports.bind = function (target) {
    const newExports = {};
    for (var key in new JobsModule()) {
        newExports[key] = oldExports[key].bind(null, target);
    }
    return newExports;
};

Declare everything as a class, then just create it whenever you want it used.将所有内容声明为一个类,然后在需要使用时创建它。 Not efficient, but probably not a big deal.效率不高,但可能没什么大不了的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM