0

I need to put a function in a javascript object, how do I add a JSDoc comment in this case?

/**
 * @typedef {Object} testObj
 * @property {!String} testName - testName
 * @property {?Function} [testFunction=null] - testFunction
 *
 */
var testObj = {
    testName:null,
    testFunction:null
}

In this form, null is the default value for the function, so it is expressed like this.

I just want to ask if it is enough to write only the @property {?Function} and write the parameter in the Object part, or if there is another way.

In the testFunction above, I want to show in JSDoc that a function with parameters {Number} a, {number} b, {Object} c is put in. If there is a way, I would appreciate it if you let me know. And I also want to express that this function returns a String.

2
  • Check out stackoverflow.com/a/17811177/1354378 Commented May 25, 2022 at 7:30
  • @Even So, does it mean that @callback should be used for elements that can contain null or Function in Object? Commented May 25, 2022 at 7:48

1 Answer 1

1

Use @function to define the function first, and then use the name of the function anywhere you need it, example:

/**
 * A test function
 *
 * @function testFunction
 * @param  {number} a Number a
 * @param  {number} b Number b
 * @param  {Object} c Object c
 * @returns {string} Returns a string
 */

/**
 * A test Object
 *
 * @typedef {Object} testObj
 * @property {!string} testName The Test Name
 * @property {?testFunction} testFunction The Test Function
 */
const testObj = {
  testName: null,
  testFunction: null,
}

Just tested it with jsdoc and the documentation is generated correctly with the link in the testFunction property.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.