Nodejsモジュールの呼び出し操作例分析
2298 ワード
本明細書の例は、Nodejsモジュールの呼び出し動作を説明する.皆さんに参考にしてあげます.具体的には以下の通りです.
User.js
User.js
//
function User(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
this.enter = function () {
console.log(this.name + " ");
}
}
/*
function User() {
this.id;
this.name;
this.age;
this.enter = function() {
console.log(this.name + " ");
}
}
*/
module.exports = User;
Techer.js
var User = require('./User');
function Teacher(id, name, age) {
User.apply(this, [id, name, age]);//
this.teach = function(res) {
res.write(this.name + " ");
}
}
module.exports = Teacher;
modal call_1.js
//----------------------n3_modalcall.js -------------
var http = require('http');
var User = require('./model/User');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
if (request.url !== "/favicon.ico") { // 2
user = new User(1, "jack", 20);
//user.id = 1;
//user.name = " ";
//user.age = 20;
user.enter();
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
modal call_2.js
//----------------------n3_modalcall.js-------------
var http = require('http');
var Teacher = require('./model/Teacher');
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
if(request.url !== "/favicon.ico") { // 2
teacher = new Teacher(1, "JackLi", 20);
teacher.enter();
teacher.teach(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
ここで述べたように、皆さんのnodejsプログラムの設計に役に立ちます.