Skip to main content

AMD Modules

Declaring an AMD module

Wrap your module in a define() call and add @module to the inner function or the returned object:

define(function () {
/**
* @module color-utils
*/

/**
* Convert an RGB value to a hex colour string.
* @param {number} r - Red (0–255).
* @param {number} g - Green (0–255).
* @param {number} b - Blue (0–255).
* @returns {string} Hex colour, e.g. `#ff6600`.
*/
function rgbToHex(r, g, b) {
return '#' + [r, g, b]
.map(v => v.toString(16).padStart(2, '0'))
.join('');
}

return { rgbToHex };
});

AMD module with dependencies

define(['underscore', 'backbone'], function (_, Backbone) {
/**
* @module app/models/user
* @requires underscore
* @requires backbone
*/

/**
* User model.
* @class
* @augments Backbone.Model
*/
var User = Backbone.Model.extend({
/**
* Default attribute values.
* @type {Object}
*/
defaults: {
name: '',
email: '',
},
});

return User;
});

Using @exports

For AMD modules that return a single function, use @exports:

define(function () {
/**
* @exports parseQuery
* @param {string} queryString
* @returns {Object}
*/
return function parseQuery(queryString) {
return Object.fromEntries(new URLSearchParams(queryString));
};
});

See also

Official reference: jsdoc.app/howto-amd-modules