Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

    • nvm

    • Node.js实例

    • Pm2

    • npm

    • 教程

      • Node.js-回调函数
      • Node.js-事件循环
      • Node.js-Buffer
      • Node.js-Stream
      • Node.js-函数
      • Node.js-模块系统
        • Node.js-模块系统
      • Node.js-路由
      • Node.js-全局对象
      • Node.js-常用工具
      • Node.js-文件系统
  • JavaScript优化

  • uniapp

  • Mini Program

  • TypeScript

  • 面向对象编程

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • 前端
  • Node.js
  • 教程
HiuZing
2023-02-17
目录

Node.js-模块系统

# Node.js-模块系统

创建 hello.js 文件

exports.world = function() {
  console.log('Hello World');
}
1
2
3

创建main.js 文件并引入 hello 模块

var hello = require('./hello');
hello.world();
1
2

在以上示例中,hello.js 通过 exports 对象把 world 作为模块的访问接口,在 main.js 中通过 require('./hello') 加载这个模块,然后就可以直接访 问 hello.js 中 exports 对象的成员函数了。

有时候我们只是想把一个对象封装到模块中

创建 hello.js 文件

function Hello() { 
    var name; 
    this.setName = function(thyName) { 
        name = thyName; 
    }; 
    this.sayHello = function() { 
        console.log('Hello ' + name); 
    }; 
}; 
module.exports = Hello;
1
2
3
4
5
6
7
8
9
10

创建main.js 文件并引入 hello 模块

var Hello = require('./hello'); 
hello = new Hello(); 
hello.setName('BYVoid'); 
hello.sayHello(); 
1
2
3
4
上次更新: 2024/08/14, 04:14:33
Node.js-函数
Node.js-路由

← Node.js-函数 Node.js-路由→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式