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/');ui