简体   繁体   English

如何描述传递的匿名函数的参数?

[英]How to describe parameters of passed anonymous function?

I want to describe curVal with JSDoc? 我想用JSDoc描述curVal吗?

I tried @param just before arrays.every() and inside anonymous callback, but it didn't help for PhpStorm to resolve methods. 我在arrays.every()之前和匿名回调内部尝试了@param ,但这对PhpStorm解析方法没有帮助。

/**
 * @param {Array} curVal
 */
arrays.every(function (curVal) {
    /**
     * @param {Array} curVal
     */     
    curVal.???
});

I need it for convinience, I just want to easily access in IDE's autocompletion Array.prototype methods on object curVal 为了方便起见,我需要它,我只想在对象的curVal IDE的自动完成Array.prototype方法中轻松访问

I think your best (and most legible) option is to give the function a name, eg 我认为最好(也是最清晰)的选择是给函数命名,例如

/**
 * @type {Number[]}
 */
var array = [ 1, 2, 3 ];

/**
 * @param {Number} curVal
 */
var fn = function (curVal) {
    // operate on curVal
};
array.forEach(fn);

I think you should (and perhaps must) specify the array type, as in my example, not not just {Array} , and I don't know what you mean by arrays.every , but I assume you meant to use .forEach from the Array object. 我认为您应该(也许必须)指定数组类型,例如在我的示例中,不仅是{Array} ,而且我不知道arrays.every是什么意思,但我假设您打算使用.forEach数组对象。

[Edit] [编辑]

OK, now I understand, maybe something like this is what you want: 好,现在我明白了,也许您想要这样的东西:

/**
 * @param {Array} curVal
 */
var fn = function (curVal) {
    curVal. // press Ctrl-Space here, and autocompletion will work
};

However, if you really want to use jsdoc with an anonymous function, something like this will work (and is closest to the code in your question): 但是,如果您真的想将jsdoc与匿名函数一起使用,则类似的方法将起作用(并且最接近您问题中的代码):

var arrayOfArrays = [[1,2,3], [2,3,4]]
arrayOfArrays.every(/** @param {Array} curVal */function (curVal) {
    curVal. // press Ctrl-Space here, and autocompletion will work
});

(I use JetBrains' IntelliJ, not their PhpStorm IDE, but they share the same Javascript integration) (我使用JetBrains的IntelliJ,而不是他们的PhpStorm IDE,但它们共享相同的Javascript集成)

I don't know how smart PhpStorm is ( the docs say it recognizes Closure Compiler tags and type annotations ), but I can think of two possible solutions. 我不知道PhpStorm有多聪明( 文档说它可以识别Closure Compiler 标签和类型注释 ),但是我可以想到两种可能的解决方案。

First is to tell it directly the type of the function param: 首先是直接告诉它函数参数的类型:

arrays.every(/** @param {Array} curVal */ function (curVal) {
  // ...
});

Or (Closure Compiler inline style): 或(Closure Compiler内联样式):

arrays.every(function (/** Array */ curVal) {
  // ...
});

Second, and this will only work if PhpStorm is smart enough to know how Array.prototype.every 's callback gets its arguments, is to make sure it knows arrays is an array of arrays: 其次,只有在PhpStorm足够聪明以知道Array.prototype.every的回调如何获取其参数的情况下,这才起作用,以确保它知道arrays是数组的数组:

/** @type {Array.<Array>} */
var arrays = getArrays();

Or: 要么:

var arrays = /** Array.<Array> */ getArrays();

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

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