標籤:node.js javascript node.js模組調用
js建立一個類
然後在另一程式中執行個體化使用這個類.
1.建立一個User類
//--------------User.js--------------
function User(id,name,age){
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />this.id=id;
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />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‘: ‘text650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />ml; 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/‘);
本文出自 “南山採菊” 部落格,請務必保留此出處http://hezudao.blog.51cto.com/6872139/1870425
node.js學習筆記_模組調用