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.
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
.
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
?
Things I've tried:
@returns {module.exports}
@returns {exports}
@module
at top, then @returns
that symbol@alias
- this unfortunately seem to completely take over the symbol, so now I no longer have anything at 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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.