1. The synchronous CommonJS
It is mainly used in node.js on the server
var x = 10;
var funcA = function(value) {returnvalue; };module.exports.x = x;
module.exports.funcA = funcA;
Copy the code
Exports and module.export
Exports: a variable (object) in itself. It is not a reference to module. It is a reference to {}, which points to the {} module of module.exports. Only. Syntax can be used to expose variables externally.
The module exports:
Module is a variable that points to a block of memory, exports is a property in module that is stored in memory, and exports is a property that points to {} modules. You can either use the. Syntax, or you can assign directly using =.
2. The asynchronous
RequireJS (AMD),SeaJS (CMD has collapsed) asynchronous, mostly used for client, AMD first load, CMD lazy loadCopy the code
- 1. Define features using require.config()
- 2. Define the module with definde()
- 3. Load the module with require()
/ / the require. Config definition:
require.config({
baseUrl: "js/lib".paths: {
"jquery": "jquery.min".// The actual path is js/lib/jquery.min.js
"math": "math.min",}});// Define the math.js module
define(function(){ xxxx; });
// Import the module module
require(['jquery'.'math'].function($, math){});Copy the code
1.AMD is required to declare before running 2Copy the code
3.ES6 module system
Export Output: variable, function, class export default (module default)Copy the code
// a.js
export let a = 1 // Output variables
export fn(){... }// Output function
export class Class{
constructor(a,b){
this.a = a;
this.b = b; }}/ / output class
fn2(){... }// Define a function
export fn2; // It is recommended to export in this way with uniform output at the end
export {fn2 ,fn}; // Export multiple variables with {}
export {fn2 as asFn2}; // Export rename with {}
// b.js
import {fn2} from 'a.js'; // add {} to import export;
import {fn2 , a , Class as Class1} from 'a.js' // Separate multiple entries with ', ';
import * as example from 'a.js'; // Import the entire module
example.a = 1; // Use modules, recommended when there is a lot to import;
// a.js
export default function(num1,num2){... }// Export the default value. Only one can be exported
export let a = 1; // Export variable A
// b.js
import any from 'a.js'; // Introduce default values,any can be any name, not {}
import {a} from 'a.js'; // add a;
import any,{a} from 'a.js' // Default values must precede non-default values
import {default as any , a} from 'a.js' // The rename is wrapped in braces
Copy the code