JavaScript modules allow you to break up your code into separate files.
This makes it easier to maintain a code-base.
Modules are imported from external files with the import
statement.
Modules also rely on type="module"
in the <script> tag.
Modules with functions or variables can be stored in any external file.
There are two types of exports: Named Exports and Default Exports.
Let us create a file named person.js
, and fill it with the things we want to export.
You can create named exports two ways. In-line individually, or all at once at the bottom.
person.js
export const name = "Jesse";
export const age = 40;
person.js
const name = "Jesse";
const age = 40;
export {name, age};
Let us create another file, named message.js
, and use it for demonstrating default export.
You can only have one default export in a file.
message.js
const message = () => {
const name = "Jesse";
const age = 40;
return name + ' is ' + age + 'years old.';
};
export default message;
You can import modules into a file in two ways, based on if they are named exports or default exports.
Named exports are constructed using curly braces. Default exports are not.
Import named exports from the file person.js:
import { name, age } from "./person.js";
Import a default export from the file message.js:
import message from "./message.js";
Modules only work with the HTTP(s) protocol.
A web-page opened via the file:// protocol cannot use import / export.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!