函数调用

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,打开浏览器

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,打开浏览器

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,打开浏览器

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

nodejs进阶(2)—函数模块调用的更多相关文章

  1. NodeJS学习笔记 进阶 (12)Nodejs进阶:crypto模块之理论篇

    个人总结:读完这篇文章需要30分钟,这篇文章讲解了使用Node处理加密算法的基础. 摘选自网络 Nodejs进阶:crypto模块之理论篇 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速 ...

  2. Nodejs进阶:核心模块net入门与实例讲解

    模块概览 net模块是同样是nodejs的核心模块.在http模块概览里提到,http.Server继承了net.Server,此外,http客户端与http服务端的通信均依赖于socket(net. ...

  3. Nodejs进阶:核心模块https 之 如何优雅的访问12306

    本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严 ...

  4. Nodejs进阶:crypto模块中你需要掌握的安全基础

    本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址. 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速度增长.同时,各类网络安全问题层出不穷.在信 ...

  5. Nodejs进阶:核心模块Buffer常用API使用总结

    本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 Buffer是node的核心模块,开发者可以利用它来处 ...

  6. nodejs进阶(3)—路由处理

    1. url.parse(url)解析 该方法将一个URL字符串转换成对象并返回. url.parse(urlStr, [parseQueryString], [slashesDenoteHost]) ...

  7. nodejs进阶(1)—输出hello world

    下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var  http  =  require ...

  8. NodeJS学习笔记 进阶 (13)Nodejs进阶:5分钟入门非对称加密用法

    个人总结:读完这篇文章需要5分钟,这篇文章讲解了Node.js非对称加密算法的实现. 摘录自网络 地址: https://github.com/chyingp/nodejs-learning-guid ...

  9. Python3基础(4)匿名函数、装饰器、生成器、迭代器、内置函数、json&pickle序列化、软件目录开发规范、不同目录间模块调用

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

随机推荐

  1. NPM (node package manager) 入门 - 基础使用

    什么是npm ? npm 是 nodejs 的包管理和分发工具.它可以让 javascript 开发者能够更加轻松的共享代码和共用代码片段,并且通过 npm 管理你分享的代码也很方便快捷和简单. 截至 ...

  2. Ubuntu下使用nvm

    写在前面:刚写着写着博客就跨年了,希望新的一年大家万事如意,一切向"前"看! 安装 wget -qO- https://raw.githubusercontent.com/crea ...

  3. 在ASP.NET Core中使用百度在线编辑器UEditor

    在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...

  4. 前端学HTTP之实体和编码

    前面的话 每天都有各种媒体对象经由HTTP传送,如图像.文本.影片以及软件程序等.HTTP要确保它的报文被正确传送,识别.提取以及适当处理.为了实现这些目标,HTTP使用了完善的标签来描述承载内容的实 ...

  5. [原]一个针对LVS的压力测试报告

    LVS 测试报告 测试计划 基本功能测试 流量压力测试 响应时间测试 配置正确性测试 灾难恢复测试 测试点 基本功能测试 客户端IP地址正确性 RealServer 访问Internet测试(包括Ip ...

  6. npm 使用小结

    本文内容基于 npm 4.0.5 概述 npm (node package manager),即 node 包管理器.这里的 node 包就是指各种 javascript 库. npm 是随同 Nod ...

  7. 【干货分享】流程DEMO-制度发文和干部任免

    流程名: 制度发文和干部任免  业务描述: 当员工在该出勤的工作日出勤但漏打卡时,于一周内填写补打卡申请.  流程相关文件: 流程包.xml  流程说明: 直接导入流程包文件,即可使用本流程  表单: ...

  8. Kotlin与Android SDK 集成(KAD 05)

    作者:Antonio Leiva 时间:Dec 19, 2016 原文链接:https://antonioleiva.com/kotlin-integrations-android-sdk/ 使用Ko ...

  9. java中的移位运算符:<<,>>,>>>总结

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...

  10. ubuntu下配置vimtab空格数

    vim ~/.vimrc  没有就创建 set tabstop=4 //4就是4个空格