nodejs进阶(2)—函数模块调用

函数调用

 1. 文件内普通函数调用

创建一个js文件命名为2_callFunction.js,其中定义一个函数fun1,向返回对象输出了一段字符串“你好,我是fun1”。

 1 //--------------------2_callFunction.js---------------------------------    
 2 var  http = require('http');       
 3 http.createServer(function    (request,    response)    {      
 4     response.writeHead(200,    {'Content-Type':    'text/html;    charset=utf-8'});      
 5     if(request.url!=="/favicon.ico"){ //清除第2此访问  
 6         fun1(response);//调用普通函数fun1
 7         response.end();    
 8     }  
 9 }).listen(8000);      
10 console.log('Server running at http://127.0.0.1:8000/');    
11 //---普通函数      
12 function  fun1(res){      
13     res.write("你好,我是fun1");      
14 }      

我们运行:node 2_callFunction,打开浏览器

2. 调用其他文件里的函数

  首先我们先创建一个otherfuns.js文件,这个文件里有两个函数,call函数被controller调用,这两个函数都向response对象输出一段字符串,函数通过module.exports提供给外部调用。这里我们只提供对外一个函数controller

 1 //-------------------models/otherfuns.js--------------------------      
 2 function  controller(res){      
 3     res.write("执行controller <br>");      
 4     call(res);      
 5     res.end();      
 6 }      
 7 function  call(res){      
 8     res.write("执行call");      
 9 }      
10 module.exports  =  controller;    //只支持一个函数      

 我们通过require将otherfuns.js引入到主文件里,require工作机制参见 require() 源码解读

下面两句都可以调用到controller函数:

 1) var other =new otherfun (response);//otherfuns.js 里的方法controller被调用
 2) otherfun (response);//otherfuns.js 里的方法controller被调用

 1 //--------------------2_callFunction.js---------------------------------  调用otherfuns.js里的函数
 2 var  http = require('http');    
 3 var  otherfun = require('./models/otherfuns.js');         
 4 http.createServer(function    (request,    response)    {      
 5     response.writeHead(200,    {'Content-Type':    'text/html;    charset=utf-8'});      
 6     if(request.url!=="/favicon.ico"){ //清除第2此访问  
 7         var other =new otherfun (response);//otherfuns.js 里的方法controller被调用
 8         //otherfun (response);//otherfuns.js 里的方法controller被调用
 9         response.end();    
10     }  
11 }).listen(8000);      
12 console.log('Server running at http://127.0.0.1:8000/');  

 我们运行:node 2_callFunction,打开浏览器

3. 提供调用多个函数的写法:

第一种:

 1 //支持多个函数    
 2 function  controller(res){      
 3     res.write("执行controller <br>");           
 4     res.end();      
 5 }      
 6 function  call(res){      
 7     res.write("执行call");  
 8     res.end();          
 9 }      
10 module.exports.controller =  controller;
11 module.exports.call =call;

第二种:

 1 //支持多个函数      
 2 module.exports={      
 3     controller:function(res){      
 4         res.write("执行controller <br>");      
 5         res.end();    
 6     },      
 7     call:function(res){      
 8         res.write("执行call <br>");      
 9         res.end();    
10     }      
11

调用方式相比只支持一个函数的方式,需要将:otherfun (response);

修改成如下调用方式

otherfun.controller(response);//otherfuns.js 里的函数controller被调用

4. 模块化调用应用(面向对象)

 我们建立一个User对象

 1 //--------------User.js--------------  
 2 function  User(id,name,age){
 3     this.id=id;//属性
 4     this.name=name;//属性
 5     this.age=age;//属性
 6     this.enter=function(){//方法
 7         console.log("进入图书馆");
 8     }
 9 }
10 module.exports  =  User;

再建一个Teacher对象

1 //-------------------models/Teacher.js---------  
2 var  User  =  require('./User');
3 function  Teacher(id,name,age){
4     User.apply(this,[id,name,age]);//继承User
5     this.teach=function(res){//自有方法
6         res.write(this.name+"老师讲课");
7     }
8 }
9 module.exports    =    Teacher; 

Teacher继承User对象,有id,name,age属性,除了enter方法外还定义了teach方法。

apply可以执行多次,所以可以继承多个对象,不如其他语言的面向对象更加严格。

在server端可以如下调用teacher。Teacher(1,'李四',30),初始化了一个实例对象

 1 //----------------------n3_modalcall.js-------------  
 2 var http = require('http');    
 3 var  Teacher  =  require('./models/Teacher');
 4 http.createServer(function (request, response)        {        
 5         response.writeHead(200, {'Content-Type': 'text/html;  charset=utf-8'});        
 6         if(request.url!=="/favicon.ico"){        //清除第2此访问
 7           teacher  =  new  Teacher(1,'李四',30);
 8           teacher.teach(response);
 9           response.end('');    
10     }
11 }).listen(8000);        
12 console.log('Server running  at  http://127.0.0.1:8000/');

我们运行:node 3_modelCall,打开浏览器

5. 用函数名的字符串调用 

otherfuns.js内容如下

1 //支持多个函数      
2 module.exports={      
3     controller:function(res){      
4         res.write("执行controller <br>");         
5     },      
6     call:function(res){      
7         res.write("执行call <br>");       
8     }      
9

再server里通过字符串调用otherfuns里的函数

 1 //-----------------用函数名的字符串调用------------------
 2 var  http  =  require('http');    
 3 var  otherfun  =  require("./models/otherfuns.js");
 4 http.createServer(function (request,response)  {        
 5         response.writeHead(200,{'Content-Type':  'text/html; charset=utf-8'});        
 6         if(request.url!=="/favicon.ico"){ 
 7           //-------用字符串调用对应的函数---
 8           funname  =  'controller';
 9           otherfun[funname](response);
10           otherfun['call'](response);
11           response.end();    
12     }
13 }).listen(8000);        
14 console.log('Server running  at  http://127.0.0.1:8000/'); 

 

posted @ 2017-01-03 16:14  方帅  阅读(13742)  评论(0编辑  收藏  举报