Node.js学习 - Modules

时间:2023-03-10 04:01:00
Node.js学习 - Modules

创建模块

当前目录:hello.js, main.js

// hello.js
exports.world = function() { // exports 对象把 world 作为模块的访问接口
console.log("Hello World!");
} // main.js
var hello = require('./hello');
hello.world();
//hello.js
function Hello() { //也可以把一个对象封装到模块中
var name;
this.setName = function(thyName) {
name = thyName;
};
this.sayHello = function() {
console.log('Hello ' + name);
};
};
module.exports = Hello;
 

模块加载流程

Node.js学习 - Modules