JS中的export与Node中的exports

一、JS 中的 export

在 JavaScript ES6 中,export 与 export default 均可用于导出常量、函数、文件、模块等。

export、export default 负责 导出,import 则负责导入。

export 在一个js文件中可以有多个,export default 最多只能有一个。

export 可以和 export default 混用。

1、export

1.1 导出声明

const aa = "aa";
function test() {
  console.log("test");
}
export { aa, test };

1.2 直接导出

export const aa = "aa";
export function test() {
  console.log("test");
}

1.3 混用

const aa = "aa";
export { aa };
export function test() {
  console.log("test");
}

1.4 导出时取别名

const aa = "aa";
function test() {
  console.log("test");
}
export { aa as a1, test };

1.5 导入

// 方式一:解构导入
import { aa, test } from './module.js'
// 方式二:导入时取别名
import { aa as a1, test } from './module.js'
// 方式三、全部导入
import * as modul from './module.js'

2、export default

2.1 导出声明

const aa = 'aa';
export default aa

2.2 导出对象

const aa = 'aa';
const bb = 'bb';
function test() {
    console.log('test')
}
export default { aa, bb, test: test() }

2.3 导出函数

const aa = 'aa';
export default function test() {
    console.log(aa)
}

2.4 导出类

export default class {
  constructor(value) {
    this.value = value
  }
  test(type) {
    console.log(value, type)
  }
}

 2.5 导入

import modul from './module.js'

// 正常使用
console.log(modul)
// 导出 class 类使用
const class_method = new modul('static')
class_method.test('test')

参考

export、export default和import用法详解_export default import-CSDN博客

二、Node 中的 exports

1、module

在每个自定义模块js中都有一个module 对象,它里面存储了和当前模块有关的信息。

// test.js
console.log(module)
node test.js

输出如下 :

Module {
  id: 'F:\\server\\utils\\test.js',
  path: 'F:\\server\\utils',
  exports: {},
  filename: 'F:\\server\\utils\\test.js',
  loaded: false,
  children: [],
  paths: [
    'F:\\server\\utils\\node_modules',
    'F:\\server\\node_modules',
    'F:\\node_modules',
  ]
}

2、module.exports

在自定义模块中,可以使用 module.exports 对象,将模块内成员共享出去,供外界使用;外界使用 require() 方法导入自定义模块时,得到的是 module.exports 所指的对象;

在一个自定义模块中,默认 module.exports = {}

2.1 导出方式一

module.exports.aa = 'aa'

2.2 导出方式二

module.exports = {
    aa: 'aa',
    test: () => {
        console.log('test')
    }
}

2.3 导入

// 方式一:直接导入
const modul = require('./modul.js')
// 方式二:解构导入
const { aa } = require('./modul.js')

3、exports

由于 module.exports 单词写起来比较复杂,为了简化,Node提供了exports 对象。默认情况下 module.exports 和 exports 指向同一个对象

3.1 导出

exports.aa = 'aa'
exports.test = () => {
    console.log('test')
}

3.2 导入

// 方式一:直接导入
const modul = require('./modul.js')
// 方式二:解构导入
const { aa, test } = require('./modul.js')

3.3 错误写法

exports = {
 aa: 'aa'
}

注意

require() 模块时,得到的永远是 module.exports 指向的对象。

示例

导出

exports.aa = 'aa'

module.exports = {
  bb: 'bb'
}

exports.cc = 'cc'

 导入

const modul = require('./modul.js')
console.log(modul)

 输出

{ bb: 'bb' }

参考

Node.js 模块化 module.exports 和 exports 的使用-CSDN博客

三、实用

导入模块时可以省略 .js 后缀

const modul = require('./modul')

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这些关键字都是用于在 JavaScript 导出模块的方式。 1. `module.exports` 是 CommonJS 规范用于导出模块的方式。在 Node.js ,`module.exports` 是一个指向当前模块导出对象的引用。可以通过给 `module.exports` 赋值来导出一个对象或函数。 例如: ``` // 导出一个函数 function add(a, b) { return a + b; } module.exports = add; ``` 2. `exports` 是 `module.exports` 的一个辅助对象,可以通过给 `exports` 对象的属性赋值来导出模块。 例如: ``` // 导出一个对象 exports.name = 'Tom'; exports.age = 18; ``` 这个模块实际上等价于: ``` module.exports = { name: 'Tom', age: 18 }; ``` 3. `export` 是 ES6 用于导出模块的关键字。可以通过 `export` 关键字导出一个变量、函数或对象。 例如: ``` // 导出一个变量 export const name = 'Tom'; // 导出一个函数 export function add(a, b) { return a + b; } // 导出一个对象 export default { name: 'Tom', age: 18 }; ``` 4. `export default` 也是 ES6 用于导出模块的关键字,但是它只能导出一个默认值。 例如: ``` // 导出一个默认值 export default function add(a, b) { return a + b; } // 导出一个默认对象 export default { name: 'Tom', age: 18 }; ``` 在导入模块时,可以使用 `import` 关键字来引入模块。例如: ``` // 导入 CommonJS 模块 const add = require('./add'); // 导入 ES6 模块 import { name, add } from './module'; import person from './person'; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值