简体   繁体   English

如何记录返回 function 调用的 function 调用(用于尾调用优化)

[英]How to document a function that returns a function call (for tail call optimization)

I'm trying to skill up on JSDoc, and I have this method:我正在尝试提高 JSDoc 的技能,我有这个方法:

/**
 * Cleanup step.
 *
 * @return {Void}
 */
beforeDestroy() {
    window.removeEventListener('resize', this.onResize);
},

That seems correct, but for my normal JavaScript coding-style, I would have this:这似乎是正确的,但对于我正常的 JavaScript 编码风格,我会这样:

beforeDestroy() {
    return window.removeEventListener('resize', this.onResize);
},

because I always try to return a function call if I can (as a functional programming habit related to tail call optimization).因为如果可以的话,我总是尝试返回 function 调用(作为与尾调用优化相关的函数式编程习惯)。 This leads me to my question of, how do I document a function that returns a function call?这引出了我的问题,我如何记录返回 function 调用的 function?

My naive estimate is it would be something like this:我天真的估计是这样的:

/**
 * Cleanup step.
 *
 * @return {(String, Function) => Void}
 */
beforeDestroy() {
    return window.removeEventListener('resize', this.onResize);
},

I'd like to hear about what correct/normal is for this kind of thing.我想听听这种事情的正确/正常情况。 I'd like to know how to document like this where the function "returns a function-call that returns void" or also something else like "returns a function-call that returns a string".我想知道如何在 function“返回一个返回 void 的函数调用”或其他类似“返回一个返回字符串的函数调用”的地方记录这样的文件。

I've seen people ask about curried functions before, and this kind of gets out of hand immediately for me with respect to documention, like how many functions does a person need?我以前看到有人问过柯里化函数,这种关于文档的问题对我来说马上就失控了,比如一个人需要多少个函数?

Something like this seems unreasonable to me:这样的事情对我来说似乎不合理:

 * @return {(String, Function) => (Object) => Void}

Like how much awareness of future execution does the "doc" need?比如“文档”需要多少对未来执行的意识?

You use a @callback (or @function );您使用@callback (或@function ); see: https://jsdoc.app/tags-callback.html见: https://jsdoc.app/tags-callback.html

For instance, something like.例如,类似的东西。

/**
 * This callback is a returned event listener
 * @callback removedCallback
 * @param {number} nameOfNumberParam
 * @param {string} nameOfStringParam
 */

/**
 * Cleanup step.
 *
 * @return {removedCallback} - The removed callback
 */
beforeDestroy() {
    return window.removeEventListener('resize', this.onResize);
},

However, you also want to ask yourself... is this really going to be useful to anyone, or am I documenting just to document?但是,您还想问自己……这真的对任何人都有用,还是我只是为了记录而记录? If what you're really going for is a type system, then Typescript can do that much better (and give you lots of other benefits besides just documentation).如果你真正想要的是一个类型系统,那么 Typescript 可以做得更好(除了文档之外,还给你很多其他好处)。

If you are just going for documentation, consider your audience.如果您只是为了文档,请考虑您的受众。 For some audiences the above style will be best, but many others might be just as well served with a simpler:对于某些观众来说,上述风格将是最好的,但对于许多其他观众来说,使用更简单的风格可能同样适用:

/**
 * Cleanup step.
 *
 * @return function - The removed callback
 */
beforeDestroy() {
    return window.removeEventListener('resize', this.onResize);
},

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

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