AMD and CommonJS are to implement a module system, which was not natively present in JavaScript until ES2015.
e.g. export
// lib.js
define(["package/lib"], function (lib) {
function foo() {
console.log( "hello foo!" );
}
// export (expose) foo
return {
foo: foo
}
});
import
// in another file
require(["package/lib"], function(myModule) {
myModule.foo();
});
e.g. export
// lib.js
function foo(){
console.log("hello foo!");
}
// export (expose) foo
exports.foo = foo;
import
// in another file
var lib = require("package/lib");
lib.foo()
e.g. export
//lib.js
export function foo() {
consol.log("hello foo!")
}
import
import lib, { foo } from 'package/lib'
lib.foo()
foo() // or just import { foo } so that we can just use it without "lib."
P.S. since the syntax keeps changing, i use the es2015 for the example. e.g. from es6, we can use arrow function to define a function