0

Let's say I have function that creates a dynamic function and returns it.

Function takes data argument of type String and returns String.

/**
 * @param {String} data
 * @returns {???}
 */
const foo = data => {
    let functionString = 'return data;'
    return Function('data', functionString)
}

I would like it to show up in VSCode Intellisense with an appropriate definition. How to document it in JSDoc?

1 Answer 1

1

You can use a namepath to document an inner member. Per the documentation:

Namepaths in JSDoc 3

When referring to a JavaScript variable that is elsewhere in your documentation, you must provide a unique identifier that maps to that variable. A namepath provides a way to do so and disambiguate between instance members, static members and inner variables.

Basic Syntax Examples of Namepaths in JSDoc 3

myFunction
MyConstructor
MyConstructor#instanceMember
MyConstructor.staticMember
MyConstructor~innerMember // note that JSDoc 2 uses a dash

Since you want to refer to the returned inner Function inside foo, you must use the very last option which documents an inner member of a function.

To use namepaths, you must use an identifier so JSDoc can identify the member, and this is achieved through declaring a variable as mentioned in the documentation. You can define your return value as a variable and return the variable. Thus:

/**
 * @param {String} data
 * @returns {foo~newFunction}
 */
const foo = data => {
    let functionString = 'return data;'

    /**
     * Notice the idenfier newFunction given to the member
     * You can now document the function here
     */
    let newFunction = Function('data', functionString)
    return newFunction
}

With the above, we define the former return value as a variable named newFunction. Consequently, we can use namepaths to reference it as foo~newFunction as its now an inner member of foo, and then set it as the return value of foo. You can then return newFunction.

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.