doctopus
doctopus

Reputation: 5647

What does @ do in this piece of code from lodash?

I've been coding in Node/React for the past year, but I'd like to dive deeper into javascript and eventually contribute to open source projects.

As a test of my abilities, I decided to look into the lodash library to see if I could understand the code.

I came across this syntax which up till now I've never seen before:

@param {Function} iteratee

I thought it was part of the comments, but it's not greyed out so figured it might be actual code. What does it mean (if anything)?

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

Upvotes: 0

Views: 58

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68655

It is used for the documentation. Using this style of documenting the code can automatically generate for you documentation based on your codes.

There are many tools/packages that let you write documentation based on the comments in their style, then using that style and code they can generate document about the API of your code. One of most popular tools is JSDoc

Some IDE's also can understand this documentation styles and show you, for example, the signature of the function or something else.

Upvotes: 1

Related Questions