1.nodejs暴露方法
如果要对外暴露属性或方法,就用 exports 就行,要暴露对象(类似class,包含了很多属性和方法),就用 module.exports。
1.简单的暴露属性和方法
exports.world = function() {console.log('Hello World');}
2.暴露对象的方法
//hello.jsfunction Hello() {var name;this.setName = function(thyName) {name = thyName;};this.sayHello = function() {console.log('Hello ' + name);};};module.exports = Hello;//main.js//调用Hello类var Hello = require('./hello');hello = new Hello();hello.setName('BYVoid');hello.sayHello();
