node.js类暴露方法

来源:互联网 发布:cmd查找他机mac 编辑:程序博客网 时间:2024/06/06 10:44

类文件

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 Peop

也就是说,js文件和js文件之间有两种合作的模式:

1) 某一个js文件中,提供了函数,供别人使用。 只需要暴露函数就行了;exports.msg=msg;

2) 某一个js文件,描述了一个类。   module.exports = People;