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

时间:2023-03-08 19:16:41
nodejs进阶(2)—函数模块调用

函数调用

1. 文件内普通函数调用

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

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

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

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

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

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

 //-------------------models/otherfuns.js--------------------------      
function  controller(res){      
    res.write("执行controller <br>");      
    call(res);      
    res.end();      
}      
function  call(res){      
    res.write("执行call");      
}      
module.exports  =  controller;    //只支持一个函数      

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

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

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

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

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

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

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

第一种:

 //支持多个函数    
function  controller(res){      
    res.write("执行controller <br>");           
    res.end();      
}      
function  call(res){      
    res.write("执行call");  
res.end();          
}      
module.exports.controller =  controller;
module.exports.call =call;

第二种:

 //支持多个函数      
module.exports={      
controller:function(res){      
    res.write("执行controller <br>");      
    res.end();    
    },      
    call:function(res){      
    res.write("执行call <br>");      
    res.end();    
    }      

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

修改成如下调用方式

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

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

我们建立一个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;

再建一个Teacher对象

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

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

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

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

 //----------------------n3_modalcall.js-------------  
var http = require('http');    
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此访问
          teacher  =  new  Teacher(1,'李四',30);
          teacher.teach(response);
          response.end('');    
    }
}).listen(8000);        
console.log('Server running  at  http://127.0.0.1:8000/');

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

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

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

otherfuns.js内容如下

 //支持多个函数      
module.exports={      
controller:function(res){      
    res.write("执行controller <br>");         
    },      
    call:function(res){      
    res.write("执行call <br>");       
    }      

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

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