您的位置:首页 > Web前端 > Node.js

nodejs学习-模块化

2020-08-24 01:42 1116 查看

模块内容导出两种方式:

**方式一:**可将需要导出的变量或函数挂载到 exports 对象的属性上
**方式二:**使用 module.exports 对象整体导出一个变量对象或者函数
例子:
index1.js

function fn(){
console.log('fn');
}

let student={
username:"yl"
}

console.log("student:",student.username)

//module.exports={student,fn}   //方式二

exports.fn=fn;//方式一

index2.js

let file1=require("./index1.js");

console.log("file1",file1);

file1.fn();

运行:




模块的引用的方式: 按照引用模块的来源来区分

// 核心模块的引入 node自己的模块
let crypto = require('crypto')

// 用户自己编写的模块引入
let aModule = require('./a.js')

// 第三方,别人实现发布的模块(其实也是其他用户编写)
let proxy = require('http-proxy');复制代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: