CommonJs standard

It is a set of standard management modules

Each js is a module, and multiple modules are a package

node.js and Couchdb are its implementations;

different from jQuery

Module: Definition, Identification, Reference (Address / Module Name)

Module type:

core module http fs path

file module var util=require('./util.js')

3rd party modules npm var promise=require('bluebird')

The flow of the module:

Create module teacher.js function foo(){}

export module exports.add=function(){} foo

Load the module another js file var teacher=require( './teacher.js')

Use module teacher.add('wangcf')

 

Source code:

student.js

function add(student){
    console.log('Add Student: ' + student)
}
// Expose the method 
exports.add=add through the exposes object

 teacher.js

function add(teacher){
    console.log('Add Teacher: ' + teacher)
}
// Expose the method 
exports.add=add through the exposes object

 

klass.js

// require reference module 
var student = require('./student' )
 var teacher = require('./teacher' )

function add(teacherName,students){
    teacher.add(teacherName)
    
    students.forEach(function(item,index){
        student.add(item);
    })
    
}
// /Both have the same function 
// The traditional module instance is the auxiliary method of 
module.exports exports.add = add
 // There is this special object type that ignores exports, the real thing 
// module.exports = add

 

index.js

var class = require ('./ class' );

klass.add( "Teacher",['Student1','Student2' ])

exports.add = function(klasses){
    klasses.forEach(function(item,index){
        var _klass = item;
        var teacherName = item.teacherName;
        var students = item.students;
        
        klass.add(teacherName,students)
    })
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325216940&siteId=291194637