The first part of this series introduces the basics of JavaScript modules and today describes how to use modules in a standardized way.
(up to the above)
Specification of Modules
First think about why the module is important?
Because of the module, we can more easily use other people's code, want what function, loaded what module.
However, this has a prerequisite, that is, we must write the same way the module, otherwise you have your writing, I have my writing, it is not a mess of the set! This is even more important given the fact that JavaScript modules are not yet officially regulated.
At present, there are two kinds of standard JavaScript modules: Commonjs and AMD. I mainly introduce AMD, but first start with Commonjs.
Eight, Commonjs
2009, US programmer Ryan Dahl created the Node.js project, which uses JavaScript language for server-side programming.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/
This sign "JavaScript modular Programming" is officially born. Because frankly speaking, in the browser environment, no module is not particularly big problem, after all, the complexity of the Web page program is limited, but on the server side, must have a module, and the operating system and other applications to interact, or simply can not program.
The modular system of Node.js is realized by reference to COMMONJS specification. In Commonjs, there is a global method require (), which is used to load modules. Assuming there is a mathematical module math.js, you can load it like this.
var math = require (' math ');
You can then invoke the method provided by the module:
var math = require (' math ');
Math.add (2,3); 5
Because this series is mainly for browser programming, does not involve node.js, so to Commonjs do not do more introduction. As long as we know here, require () is used to load the module.