js创建一个类
然后在另一程序中实例化使用这个类
1.创建一个User类
//--------------User.js--------------
function User(id,name,age){
this.id=id;
this.name=name;
this.age=age;
this.enter=function(){
console.log("进入图书馆");
}
}
module.exports = User;
2.调用
//----------------------n3_modalcall.js-------------
var http = require(‘http‘);
var User = require(‘./models/User‘);
http.createServer(function (request, response) {
response.writeHead(200, {‘Content-Type‘: ‘textml; charset=utf-8‘});
if(request.url!=="/favicon.ico"){ //清除第2此访问
user = new User(1,‘张三‘,30); //创建一个user
user.enter();
response.end(‘‘);
}
}).listen(8000);
console.log(‘Server running at http://127.0.0.1:8000/‘);