手册
目录
JavaScript modules allow you to break up your code into separate files.
This makes it easier to maintain the code-base.
ES Modules rely on the import and export statements.
You can export a function or variable from any file.
Let us create a file named person.js, and
fill it with the things we want to export.
There are two types of exports: Named and Default.
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 must be destructured using curly braces. Default exports do 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";
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
精选课程
共5课时
17.2万人学习
共49课时
77.1万人学习
共29课时
61.8万人学习
共25课时
39.3万人学习
共43课时
71万人学习
共25课时
61.7万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125.2万人学习