Javascript 函数成员导出

同级目录下

文件1

// exportByModule.js
const hello_1 = () => {
  console.info('hello_1')
}

const hello_2 = () => {
  console.info('hello_2')
}

module.exports = {
  hello_1,
  hello_2
}

// 成员导出 module.exports 优先,所以下面的代码无效
exports.hello_3 = () => {
  console.info('hello_3')
} 

文件2

// exportOneByOne.js
exports.hello_4 = () => {
  console.info('hello_4')
}

exports.hello_5 = () => {
  console.info('hello_5')
}

使用

const members = require('./exportOneByOne')
const modules = require('./exportByModule')
modules.hello_1() // hello_1
modules.hello_2() // hello_2
// modules.hello_3() // hello_3  //无法引出
members.hello_4() // hello_4
members.hello_5() // hello_5
Keep learning
原文地址:https://www.cnblogs.com/leslie1943/p/13359432.html