Java Reference
In-Depth Information
supplied, assume the lower limit is 1
return Math.floor((b-a+1) * Math.random()) + a;
}
This is simply the random() function written as an anonymous function that's assigned
to module.exports as if it was a variable.
To use the module, it needs to then be required inside the main scripts.js file. This is done
using the require() method, and takes the file that contains the module as an argument
and returns the function that was exported:
var random = require('./random');
The function that was exported in the module is now assigned to the variable random ,
which is then used to call the function:
random(6);
<< 4
Asynchronous Module Definitions
Asynchronous Module Definitions (AMD) modules are most commonly used by Re-
quireJS, which is a file and module loader designed for use in the browser. The AMD mod-
ule syntax is slightly more complicated than the CommonJS module, but it's designed to
support asynchronous loading of any modules and their dependencies.
AMD modules are written in a separate file and provided as an argument to the define()
function. The following code is an example of how the random() function would be
defined as an AMD module in a file called random.js:
define({
random: function (a, b) {
if (b===undefined) b = a, a = 1; // if only one
argument
is supplied, assume the lower limit is 1
return Math.floor((b - a + 1) * Math.random()) +
a;
Search WWH ::




Custom Search