Node.js学习


创建模块

当前目录: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;
 

模块加载流程


 

KEEP LEARNING!
原文地址:https://www.cnblogs.com/roronoa-sqd/p/5397168.html