日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

彻底搞懂 module.exports/exports/import/export/export default

發(fā)布時(shí)間:2023/12/13 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 彻底搞懂 module.exports/exports/import/export/export default 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

module.exports/exports

module.exports 是模塊系統(tǒng)創(chuàng)建的(全局內(nèi)置對(duì)象);當(dāng)你創(chuàng)建完成一個(gè)模塊時(shí),需要將此模塊暴露出去,以便使用;module.exports 便提供了暴露出去的接口方法;

例如暴露出去一個(gè)對(duì)象(暴露一個(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'));

說(shuō)到底那 module.exports 和 exports 有啥區(qū)別呢?

  1. 語(yǔ)法區(qū)別:

exports.[function name] = [function name]

moudle.exports= [function name]

  2. 本質(zhì)區(qū)別:

  exports 暴露出的是一個(gè)模塊中的某個(gè)函數(shù);

  module.exports 暴露出模塊對(duì)象本身(其實(shí)就是一個(gè)類);

  3. 使用區(qū)別:

  exports 暴露出的函數(shù)可以直接調(diào)用;

  module.exports 暴露出的類需要實(shí)例出對(duì)象;

注意:當(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è)對(duì)象并且都有 fn 方法;module.exports === exports 結(jié)果為 true,說(shuō)明兩者指向同一個(gè)對(duì)象;

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 地址指向還是原來(lái)對(duì)象;

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)識(shí)(module);模塊定義(exports);模塊引入(require)

node在執(zhí)行一個(gè)文件時(shí),會(huì)在文件中生成一個(gè)exports 和 module 對(duì)象,module 對(duì)象又有一個(gè)exports 屬性。

exports 和 module.exports 的關(guān)系:兩者都是指向同一個(gè)對(duì)象;

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è);
通過(guò)export方式導(dǎo)出,在導(dǎo)入時(shí)要加{ },export default則不需要;
export能直接導(dǎo)出變量表達(dá)式,export default不行;

總結(jié)

以上是生活随笔為你收集整理的彻底搞懂 module.exports/exports/import/export/export default的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。