1

I want to document a new method to an internal Array objects, so that I can use the method everytime I make an array. For example:

/**
* @<whatsInHere?> Array
* @returns {number}
*/
Array.prototype.avg = function () {
    let sum = 0;
    for (let _x = 0; _x < this.length; _x++) sum += this[_x];
    return sum / this.length;
}

const myArray = [3,4,5,6,34,5,2];
const average = myArray.avg();

What should I do to document avg?

Thank you;

UPDATE: I've found about @external here and modified mine like this:

/**
* @external Array
*/

/**
* @functions external:Array#avg
*
*/
Array.prototype.avg = function() {}

Did not work either. Or perhaps, I use it the wrong way?

Magician
  • 1,944
  • 6
  • 24
  • 38
  • Maybe this can help: https://stackoverflow.com/questions/27343152/jsdoc-how-to-document-prototype-methods – Rajesh Sep 19 '22 at 05:03
  • @Rajesh yeah.. tried that. Not working for built-in objects such as Array or String. I've also read about external, but it's either I don't know how to use it properly, or wrong keyword – Magician Sep 19 '22 at 07:05

1 Answers1

-1

I think I've found a workaround, but using typescript. Just put it under <yourfile>.d.ts and it would work in javascript as well.

array.d.ts

interface Array<T> {
    avg(): T
    myAwesomeMethod(fn: (param:T)=>T):T
    .
    .
}

code.js

Array.prototype.avg = function() { }
Array.prototype.myAwesomeMethod = function(fn) { }
Magician
  • 1,944
  • 6
  • 24
  • 38