DDR爱好者之家 Design By 杰米

本文实例讲述了Nodejs模块的调用操作。分享给大家供大家参考,具体如下:

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;

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;

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

modalcall_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程序设计有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米