Fork me on GitHub

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;
 

模块加载流程

 

 

 

 


 

 

posted @ 2016-04-15 22:40  Roronoa__Zoro  阅读(202)  评论(0)    收藏  举报