exports

暴露函数

var bar = require("./bar.js");
var msg = "你好";
var info = "呵呵";

function showInfo(){
    console.log(info);
}

exports.msg = msg;
exports.info = info;
exports.showInfo = showInfo;

使用

var foo = require("./test/foo.js");

console.log(foo.msg);
console.log(foo.info);
foo.showInfo();

暴露类

function People(name,sex,age){
    this.name = name;
    this.sex = sex;
    this.age = age;
}

People.prototype = {
    sayHello : function(){
        console.log(this.name + this.sex + this.age);
    }
}

//此时,People就被视为构造函数,可以用new来实例化了。
module.exports = People;

使用

var People = require("./test/People.js");
var xiaoming = new People("小明","男","12");
xiaoming.sayHello();
原文地址:https://www.cnblogs.com/Erick-L/p/7768946.html