今天学习了,node调用模块的方法
node调用模块
要调用模块的js
1 var http = require("http"); 2 3 var Teacher = require("./Teacher"); 4 5 http.createServer(function(request,response) 6 { 7 8 response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); 9 10 if(request.url!=='/favicon.ico'){ 11 12 teacher = new Teacher(1,"张三老师",30); 13 14 teacher.enter(); 15 16 teacher.teach(response); 17 18 response.end(); 19 20 } 21 22 }).listen(8000); 23 24 console.log('Server running at http://127.0.0.1:8000/');
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
1 var User =require("./User"); 2 function Teacher(id,name,age){ 3 //Teacher通过apply继承User的属性 4 User.apply(this,[id,name,age]); 5 this.teach=function(res){ 6 res.write(this.name+"教学"); 7 } 8 } 9 10 module.exports = Teacher;
以上是源码。