彻底搞懂 module.exports/exports/import/export/export default
module.exports/exports
module.exports 是模塊系統(tǒng)創(chuàng)建的(全局內(nèi)置對象);當(dāng)你創(chuàng)建完成一個(gè)模塊時(shí),需要將此模塊暴露出去,以便使用;module.exports 便提供了暴露出去的接口方法;
例如暴露出去一個(gè)對象(暴露一個(gè)全局變量或方法):
/**創(chuàng)建模塊 module.exports.js */
let object = {
name: 'zhangsan',
age: 24,
hasSay: () => {
console.info('This is zhangsan')
}
}
module.exports = object;
/**引用模塊 server.js */
const Obj = require('./module.exports.js')
console.log('---->', Obj)
或者:
/**創(chuàng)建模塊 module.exports.js */
function fn1 () {
console.info('fn1');
}
function fn2 () {
console.info('fn2');
}
exports.fn1 = fn1;
exports.fn2 = fn2;
/**引用模塊 server.js */
const fn = require('./module.exports.js')
console.log('---->', fn.fn1, fn.fn2);
暴露出去構(gòu)造函數(shù)(類):
/**創(chuàng)建模塊 module.exports.js */
class Person {
constructor (name) {
this.name = name
console.info('name:', name);
}
}
module.exports = Person;
/**引用模塊 server.js */
const Person = require('./module.exports.js')
console.log('---->', new Person('lisi'));
說到底那 module.exports 和 exports 有啥區(qū)別呢?
1. 語法區(qū)別:
exports.[function name] = [function name] moudle.exports= [function name]
2. 本質(zhì)區(qū)別:
exports 暴露出的是一個(gè)模塊中的某個(gè)函數(shù);
module.exports 暴露出模塊對象本身(其實(shí)就是一個(gè)類);
3. 使用區(qū)別:
exports 暴露出的函數(shù)可以直接調(diào)用;
module.exports 暴露出的類需要實(shí)例出對象;
注意:當(dāng) module.exports 和 exports 同時(shí)存在于一個(gè)模塊中時(shí),以 module.exports 暴露出去的方法為主;
exports
/**創(chuàng)建模塊 module.exports.js */
function fn () {
console.info('fn');
}
exports.fn = fn;
console.log('module.exports->', module.exports); // Object { fn: }
console.log('exports->', exports); // Object { fn: }
console.log('查看兩者是否相等:', module.exports === exports); // true
module.exports 和 exports 是一個(gè)對象并且都有 fn 方法;module.exports === exports 結(jié)果為 true,說明兩者指向同一個(gè)對象;
module.exports
/**創(chuàng)建模塊 module.exports.js */
function fn () {
console.info('fn');
}
module.exports = fn;
console.log('module.exports->', module.exports); // fn () {...}
console.log('exports->', exports); // {}
console.log('查看兩者是否相等:', module.exports === exports); // false
此時(shí)module.exports 和 exports 兩者的指向不同;module.exports 地址指向 fn 方法;exports 地址指向還是原來對象;
export / import / export default
CommonJs 規(guī)范(module.exports / exports ; require);
ES6(export / import);
require : node 和 ES6 都支持的導(dǎo)入;
export / import: ES6支持的導(dǎo)入和導(dǎo)出;
module.exports / exports:node支持的導(dǎo)出;
Node
我們需要知道 Nodejs 里面的模塊系統(tǒng)都是遵循 CommonJs 規(guī)范的;
CommonJs 定義的模塊分為:模塊標(biāo)識(module);模塊定義(exports);模塊引入(require)
node在執(zhí)行一個(gè)文件時(shí),會在文件中生成一個(gè)exports 和 module 對象,module 對象又有一個(gè)exports 屬性。
exports 和 module.exports 的關(guān)系:兩者都是指向同一個(gè)對象;
exports = module.exports = {}
ES6中模塊的導(dǎo)出 / 導(dǎo)入
導(dǎo)出 export / export default 兩者的區(qū)別:
export與export default均可用于導(dǎo)出常量、函數(shù)、文件、模塊等;
在一個(gè)文件或模塊中,export、import可以有多個(gè),export default僅有一個(gè);
通過export方式導(dǎo)出,在導(dǎo)入時(shí)要加{ },export default則不需要;
export能直接導(dǎo)出變量表達(dá)式,export default不行;
總結(jié)
以上是生活随笔為你收集整理的彻底搞懂 module.exports/exports/import/export/export default的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十二、入侵防御系统
- 下一篇: 信用卡提前还款会影响征信吗?不要抱有侥幸