Saturday, September 29, 2018

jsdoc - Javascript documentation function param tutorials with examples

In my previous post, we learned javascript documentation framework - JSDOC tutorials, In this post, We are going to learn JSdoc function tags with examples.
jsdoc @param example

function documentation

Any function can be declared with function name return types and accepted input types. @param tag provides parameters for a javascript function. This includes parameter type in open and close braces and Description of a parameter

Type of the accepted parameter are string or Object or namespace pointed to code.

@param tag 

 The below examples specifies how How to document javascript function names, types and descriptions of a function using @param tag Below is a Function name documentation example
/**
 * @param This is Simple function in javascript
 */
function method1() {
    console.log('Hi Simple Function');
}
Below is a Function name and type documentation example


/**
 * @param {string } This is Simple function in javascript
 */
function method1(msg) {
    console.log('Hi Simple Function'+msg);
}
Below is a Function name, type and description example
/**
 * @param {string } method1- simple function in javascript
 */
function method1(msg) {
    console.log('Hi Simple Function'+msg);
}

Optional parameters and the Default value

 The function can be applied with optional parameters and default values. documentation of a function can also include optional and default values.
/**
 * @param {string} [name= Kiran] - Function with optional Default parameters.
 */
function functionOptionalDefault(name) {
    if (!name) {
        name = 'Kiran';
    }
    console.log('Hi ' + name);
}
function parameter union example union type is complex type on which multiple types can be applied using symbol |


/**
 * @param {(string|string[])} [name=Kiran] - String or multiple strings
 */
function mymethod(name) {}

callback functions example 

 In javascript, Function parameter can accept callback functions. @callback tag is used to specifies callback type


/**
 * mycallback is callback function defined
 *
 * @callback mycallback
 * @param {string} parameters string
 */

/**
 * Callback handles certain operations
 * @param {mycallback} mycallback - Function with callback example
 */
function functioncallbackexample(mycallback) {
    // code
};

Related article


EmoticonEmoticon