/**
* Created by admin on 2017/11/3.
*/
//方法一,world函数是模块的访问接口
exports.world = function () {
console.log("hello,world");
};
hi.js
/**
* Created by admin on 2017/11/3.
*/
//方法二,hi对象封装到模块中,在其他地方就可以直接引入这个对象
function HI() {
var name;
this.setName = function (thyName) {
name = thyName;
};
this.sayHello = function () {
console.log('Hello '+name);
};
};
module.exports = HI;
module.js
/**
* Created by admin on 2017/11/3.
*/
//一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码、JSON 或者编译过的C/C++ 扩展。
//require引入模块,exports导出模块
//require的查找循序:文件模块缓存区=》原生模块=》文件模块=》查找到后载入模块=》缓存模块
//引入hello.js
var hello = require('./hello');
hello.world();
//引入hi.js
var hi = require('./hi');
var hi_1 = new hi();
hi_1.setName("hehe");
hi_1.sayHello();