Module
Outline
Before ES6, the community developed a number of modules to load programs, the most important are CommonJS and AMD two kinds. The former is for the server, which is used browser. ES6 on the level of language standards, to achieve the function module, the module can determine dependencies, as well as input and output variables of the design ideas ES6 module is to minimize static, making compilation. CommonJS and AMD modules, these things can only be determined at runtime. For example, CommonJS module is the object, the object must find the property you type.
1 |
|
The above is the whole essence of the code module loaded fs (fs i.e. all methods of loading), to produce an object (_fs), and then read the object 3 from the above method. This load is called "load running" because the object is only to get run-time, resulting in absolutely no way to do a "static optimization" at compile time.
ES6 module is not the object, but the code is explicitly specified by the export command output through the import command input.
1 |
// ES6模块 |
Substance of the above code is loaded from 3 fs module method, other methods are not loaded. This load is called "Loading a compile-time" or static load that can be completed ES6 module is loaded at compile time, high loading efficiency than CommonJS module.
Strict Mode
ES6 modules automatically use strict mode, whether you have add "use strict" in the module header ;.
Strict mode mainly has the following limitations:
- Variables must be declared before use
- Function parameters can not have the same name attribute, otherwise an error
- You can not use the with statement
- You can not assign read-only attribute, otherwise an error
- You can not use the prefix 0 indicates octal number, otherwise an error
- You can not delete the property can not be deleted, otherwise an error
- You can not delete variables delete prop, will complain, can only remove property delete global [prop]
- eval not introduce variable in its enclosing scope
- eval and arguments can not be reassigned
- arguments do not automatically reflect changes in function parameters
- You can not use arguments.callee
- You can not use arguments.caller
- Prohibit this point to the global object
- You can not use fn.caller and fn.arguments get the function call stack
- An increase of reserved words (such as protected, static and interface)
export order
export command is used to specify the external interface module
1 |
// output variables |
import command
import command input function provided on another module, the import command with lifting effect, will be raised to the head of the whole module is first performed
1 |
import {firstName, lastName, year} from './index'; |
as keywords
1 |
// Re-enter a name for the variable of |
export default command
Designated as the default output module
1 |
// export-default.js |
chestnut
Vuex a module (Module1) Example
1 |
//com.js |
Original: Large column reading notes -es6Module