modules are JS modules. My understanding is that when there are a large number of data and functions of the same type or related, methods need to be displayed as a whole, It can be defined separately as a module, that is, module . The meaning of Module is to modularize the code, dividing your code into logically independent subsets. Each subset handles specific functions and is then called separately.
automatically adopt strict mode, regardless of whether "user strict" is added to the module header, agruments will not automatically Reflect changes in function parameters, and prohibit this from pointing to the global object
modules has two keywords, Imports and ExportsA module is an independent File , all variables inside the file cannot be obtained externally . If you want to externally read a variable inside the module, you must use the export keyword to output the variable, export, in addition to output Variables can also output functions, class
let a='a'; let b='b'; let c='c'; export {a,b,c}
as keyword in the import command and change the name to a, for example:
import {name as a} from '.../xxx.js'
, import has a promotion effect , no matter where it is referenced, it will be promoted to the top of the entire module , first execute
in addition to the specified load A certain value can also be loaded as a whole, that is, an asterisk (*) is used to specify an object, and all output values are loaded on this object. For exampleimport * as a from '.../xxx.js' console.log(a.area(4)); console.log(a.cire(4));
export default, set the default output of the module file, each module is only allowed to have one default output, the default output is not required Know the variable name of the module. In addition, export default does not need to add braces , because, in essence, export default outputs a variable or method called default, and then the system allows you to get it Any name
var name="李四"; export { name } //import { name } from "/.a.js" 可以写成: var name="李四"; export default name //import name from "/.a.js" 这里name不需要大括号
// modules.js function add(x, y) { return x * y; } export {add as default}; // 等同于 // export default add; // app.js import { default as xxx } from 'modules'; // 等同于 // import xxx from 'modules';
import {b} from '.../xxx.js' import {c} from '.../xxx.js' //等同于 import {b,c} from '.../xxx.js'