码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • module.exports和exports


    module.exports 对象

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

    1. //在一个自定义模块中,默认情况下module.exports = {}
    2. // 向 module.exports 对象上挂载 username 属性
    3. module.exports.username = 'zs'
    4. // 向 module.exports 对象上挂载 sayHello 方法
    5. module.exports.sayHello = function() {
    6. console.log('Hello!')
    7. }
    8. const age = 20
    9. module.exports.age = age
    10. // 在外界使用 require 导入一个自定义模块的时候,得到的成员
    11. // 就是 那个模块中,通过 module.exports 指向的那个对象
    12. const m = require('./自定义模块.js')
    13. console.log(m)//{lusername:'zs',sayHello:[Function],age:20 }

    ps:使用require方法导入模块时,导入结果永远以module.exports指向对象为准

    1. //模块
    2. module.exports.username = 'zs'
    3. module.exports={
    4. age:'20'
    5. }
    6. //使用
    7. const m = require('./自定义模块.js')
    8. console.log(m)//{age:20 }

    exports 对象

    由于 module.exports 单词写起来比较复杂,为了简化向外共享成员的代码,Node 提供了 exports 对象。默认情况下,exports 和 module.exports 指向同一个对象。最终共享的结果,还是以 module.exports 指向的对象为准。(exports === module.exports)

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

    1. //-------------1-------------------
    2. //模块
    3. exports.username = 'zs'
    4. module.exports={
    5. age:'20'
    6. }
    7. //使用
    8. const m = require('./自定义模块.js')
    9. console.log(m)//{age:20 }
    10. //--------------2------------------
    11. //模块
    12. module.exports.username = 'zs'
    13. exports={
    14. age:'20'
    15. }
    16. //使用
    17. const m = require('./自定义模块.js')
    18. console.log(m)//{username:'zs' }
    19. //--------------3------------------
    20. //模块
    21. exports.username = 'zs'
    22. module.exports.age = 20
    23. //使用
    24. const m = require('./自定义模块.js')
    25. console.log(m)//{username:'zs', age:20 }

  • 相关阅读:
    maven 包管理平台-01-maven 入门介绍 + Maven、Gradle、Ant、Ivy、Bazel 和 SBT 的详细对比表格
    Qt http get请求数据阻塞和非阻塞实现源码
    java(面向对象)的23种设计模式(10)——模板方法模式
    springboot整合其它项目
    4.12每日一题(通过全微分判断多元函数最大最小值)
    3 Thymeleaf 常用语法
    【Vue】style和class 列表渲染 使用v-for进行循环 监控失效 双向数据绑定 过滤案例 事件修饰符
    Vue中使用Web Serial API连接串口,实现通信交互
    工程课 SpringBoot-2.1. 笔记
    第一章:为什么要并行计算
  • 原文地址:https://blog.csdn.net/qq_35181466/article/details/134401076
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号