JSDoc
JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they’re creating. https://jsdoc.app/index.html
Object
var foo; let bar; const FOO_BAR; let obj = {}; var arr = [];
var VS let
console.log(foo); //undefined var foo = 5; console.log(foo); //5 console.log(bar); //error let bar = 5; console.log(bar); //5
Namespace
/** @namespace */ let webapp = {};
Class
/** @class */ webapp.Util = function () { //property /** @type{Number} */ foo: 0; //method /** * @param {Number} e e. * @returns {Number} e + 1. */ bar: function(e) { return e + 1; } }; //Cara penggunaan var obj = new webapp.Util(); var a = obj.foo; //0 /** @type{Number}*/ var b; b = obj.bar(4); //5
Static Class
/** @class */ webapp.Util = function () { }; //property /** @type{Number} */ webapp.Util.foo = 0; //method /** * @param {Number} e e. * @returns {Number} e + 1. */ webapp.Util.bar = function(e) { return e + 1; }; //cara penggunaan let a = webapp.Util.foo; //0 var b = webapp.Util.bar(5); //6
String
/** @type{String} */ var fooBar; let foo = 'Foo'; let bar = 'Bar'; fooBar = foo + ' ' + bar; //Foo Bar fooBar = `${foo} ${bar}`; //Foo Bar
Define Object
//gunakan [] untuk nullable /** * @callback webapp.Util.FooBarCallback * @param {String} e e. * @returns {void} */ /** * @typedef {Object} webapp.Util.FooBarParam * @property {String} foo * @property {String} bar * @property {webapp.Util.FooBarCallback} [callback] */ /** * @param {String|webapp.Util.FooBarParam} args args. * @returns {String} */ webapp.Util.joinFooBar = function(args) { /** @type {String} */ var fooBar; /** @type {webapp.Util.FooBarParam} */ var param = {}; switch (typeof args) { case 'string': param.foo = args; break; case 'object': param.foo = args.foo; param.bar = args.bar; param.callback = args.callback; break; default: console.error(`Unexpected type of argument! Expected String or Object, got ${typeof args}`); return; } if (typeof param.bar === 'string') { fooBar = `${param.foo} ${param.bar}`; } else { fooBar = param.foo; } if (typeof param.callback === 'function') { param.callback(fooBar); } return fooBar; };