路径:
│ 03_modelcall.js
│
└─ models
│ Teacher.js
└─ 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 + "进入图书馆");
}
}
module.exports = User;
Teacher.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;
03_modelcall.js
var http = require('http');
var User = require('./models/User');
var Teacher = require('./models/Teacher');
http.createServer(function(request, response) {
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
if (request.url !== "/favicon.ico") { //清除第2此访问
var teacher = new Teacher(1, '李四', 30);
teacher.teach(response);
var user = new User(1, '李四', 30);
user.enter()
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');