过去一年我一直在 Node/React 中进行编码,但我想更深入地研究 JavaScript,并最终为开源项目做出贡献。
为了测试我的能力,我决定研究一下 lodash 库,看看我是否能理解代码。
我遇到了迄今为止我从未见过的语法:
@param {Function} 迭代器
我认为这是评论的一部分,但它没有变灰,所以认为它可能是实际的代码。这意味着什么(如果有的话)?
lodash map.js
31 lines (28 sloc) 736 Bytes
/**
* Creates an array of values by running each element of `array` thru `iteratee`.
* The iteratee is invoked with three arguments: (value, index, array).
*
* @since 5.0.0
* @category Array
* @param {Array} array The array to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns the new mapped array.
* @example
*
* function square(n) {
* return n * n
* }
*
* map([4, 8], square)
* // => [16, 64]
*/
function map(array, iteratee) {
let index = -1
const length = array == null ? 0 : array.length
const result = new Array(length)
while (++index < length) {
result[index] = iteratee(array[index], index, array)
}
return result
}
export default map
最佳答案
它用于文档。使用这种记录代码的方式可以根据您的代码自动生成文档。
有许多工具/包可以让您根据其风格中的注释编写文档,然后使用该风格和代码可以生成有关代码 API 的文档。最流行的工具之一是JSDoc
某些 IDE 还可以理解此文档样式并向您显示例如函数的签名或其他内容。
关于javascript - @ 在 lodash 的这段代码中做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49435776/