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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM