export 和module.export的区别
时间: 2023-08-12 19:05:34 浏览: 71
export和module.exports都是用于导出模块的关键字,但是在使用上有一些区别。
1. export是ES6中的关键字,用于导出模块中的变量、函数或类等,可以导出多个成员,使用时需要使用import进行引入。
2. module.exports是Node.js中的关键字,用于导出整个模块,可以导出一个对象或者一个函数,只能导出一个成员,使用时需要使用require进行引入。
总的来说,export适用于ES6模块化开发,而module.exports适用于Node.js模块化开发。
相关问题
export和module.exports区别
export和module.exports都是用于导出模块的关键字,但是在使用上有一些区别。
1. export是ES6中的关键字,用于导出模块中的变量、函数或类等,可以导出多个成员,使用时需要使用import进行引入。
2. module.exports是Node.js中的关键字,用于导出整个模块,可以导出一个对象或者一个函数,只能导出一个成员,使用时需要使用require进行引入。
总的来说,export适用于ES6模块化开发,而module.exports适用于Node.js模块化开发。
export、module.export、export default
export、module.exports和export default是用于在JavaScript模块中导出变量、函数或对象的关键字。
1. export关键字用于导出一个或多个变量、函数或对象。可以使用命名导出和默认导出两种方式。
- 命名导出:使用export关键字导出多个变量、函数或对象,需要使用相应的名称进行导入。
```javascript
// 导出变量
export const name = 'John';
export const age = 25;
// 导出函数
export function sayHello() {
console.log('Hello!');
}
// 导出对象
export const person = {
name: 'John',
age: 25
};
```
- 默认导出:使用export default关键字导出一个默认的变量、函数或对象,不需要使用相应的名称进行导入。
```javascript
// 导出默认变量
export default 'Hello!';
// 导出默认函数
export default function sayHello() {
console.log('Hello!');
}
// 导出默认对象
export default {
name: 'John',
age: 25
};
```
2. module.exports是CommonJS模块系统中用于导出变量、函数或对象的方式。
```javascript
// 导出变量
module.exports.name = 'John';
module.exports.age = 25;
// 导出函数
module.exports.sayHello = function() {
console.log('Hello!');
};
// 导出对象
module.exports.person = {
name: 'John',
age: 25
};
```
总结:
- export和module.exports是ES6和CommonJS模块系统中用于导出的关键字,用法略有不同。
- export可以导出多个变量、函数或对象,而export default和module.exports只能导出一个。
- 使用export导出的变量、函数或对象需要使用相应的名称进行导入,而使用export default和module.exports导出的默认值可以使用任意名称进行导入。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)