node.js学习笔记_模块调用

原创 2016-11-08 10:31:56 381
摘要:js创建一个类然后在另一程序中实例化使用这个类. 1.创建一个User类//--------------User.js--------------  function  User(id,name,age){    this.id=id;    this.name=nam

js创建一个类

然后在另一程序中实例化使用这个类.

 
1.创建一个User类
//--------------User.js--------------  
function  User(id,name,age){
    this.id=id;
    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':        'textml;        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/');


发布手记

热门词条