Because the new job to do front-end, as the front-end small white and can learn as soon as possible, so choose the web of node. JS's Introductory tutorial. Blogs only do their own records of learning.
JavaScript lacks a module management mechanism, which is prone to variables being overwritten and methods being substituted (both contaminated). In particular, there are dependencies that are prone to errors. In the way of namespaces, variables and functions are limited to a specific scope, human flesh contracts a set of naming conventions to restrict code, to ensure that the code runs safely.
Commonjs is a set of specifications that includes modules,system,encodings,unit test and so forth to agree on how JavaScript should be organized and written.
The code blocks and code files that perform different tasks are looked at first as separate modules, each of which is a separate scope, but not isolated, and there may be dependencies. Each module is divided into three parts, defined, identified, and referenced. node. JS draws on Commonjs's philosophy and implements a modular management system based on COMMONJS.
Nodejs inside, each JS file can be seen as a separate module, there is no need to have a namespace, there is no need to worry about variable pollution and method isolation, and each module through the combination of dependency and introduction to form a more powerful function package
(a) Module classification:
Core modules
File module
Third-party modules
(ii) Introduction of modules:
Path Introduction
Name Introduction
(iii) Use of the module process:
Create module: JS file of Teacher.js entry, and add specific function
Export module: Exports.add=function () {} Set this function in relation to the module name
Load module: Var teacher=require ('./teacher.js ') introduces and loads modules in other files
Using modules: Teacher.add (' Scott ')
(d) Code test:
Directory structure:
School
----Student.js
----Teacher.js
----Class.js
----Index.js Portal File
Student.js:
function Add (student) {
Console.log (' Add student: ' + student)
}
Exports.add = Add
Code parsing:
Exports is an object that can mount any legitimate JavaScript object, including attributes, Number,date,json,string,array, etc.
Klass.js:
var student = require ('./student ')
var teacher = require ('./teacher ')
Teacher.add (' Scott ')
function Add (teachername, students) {
Teacher
Students.foreach (function (item,index) {
Student.add (item)
})
}
Exports.add = Add
Can directly require module, do not worry about affecting the global namespace, node has no global namespace concept, if the module is not error, require will return the module object, this module object is the module exports, You can also use local objects to receive objects returned by require
If you want the module to be a special object type, use Module.exports if you want the module to be a traditional module instance, use Exports.add (recommended)
Index.js:
var Klass = require ('./klass ')
Exports.add = function (klasses) {
Klasses.foreach (function (item, index) {
var _klass = Item
var teachername = Item.teachername
var students = Item.students
Klass.add (teachername,students)
})
}var Klass = require ('./klass ')
Klass.add (' Scott ', [' Lucy ', ' Ben '])
node. js Getting Started Tutorial (ii): modules