函数调用

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. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  2. C# 破解 Reflector8.5

    一.分析 破解.net .dll,可以使用reflector,但官方提供的reflector是需要购买的,因此,破解reflector势在必行. 二.破解Reflector具体步骤 下面为详细的破解步 ...

  3. VisualStudio2013 如何打开之前版本开发的(.vdproj )安装项目

    当你的项目使用早于 visualstudio2013 的版本开发并且使用 Visual Studio Installer 制作安装项目时,在升级至 VS2013 后会发现新安装项目无法打开, VS20 ...

  4. CentOS7 重置root密码

    1- 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在按下 Con ...

  5. Java多态性——分派

    一.基本概念 Java是一门面向对象的程序设计语言,因为Java具备面向对象的三个基本特征:封装.继承和多态.这三个特征并不是各自独立的,从一定角度上看,封装和继承几乎都是为多态而准备的.多态性主要体 ...

  6. js闭包 和 prototype

    function test(){ var p=200; function q(){ return p++; } return q; } var s = test(); alert(s()); aler ...

  7. bzoj3208--记忆化搜索

    题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目.    我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...

  8. js attribute 和 jquery attr 方法

    attribute 是原生js dom 对象上的一个属性,这个属性有很多子属性,比如 isId(判断属性是否是Id) , name (获取属性名称) , value (获取属性值),attribute ...

  9. Register-SPWorkflowService 404

    最近需要做一个SharePoint 2013工作流演示环境. 于是在自己的本子上安装了一个虚拟机. 虚拟机操作系统是Windows Server 2012 R2,计划把AD.SQL Server 20 ...

  10. Idea下用SBT搭建Spark Helloworld

    没用过IDEA工具,听说跟Eclipse差不多,sbt在Idea其实就等于maven在Eclipse.Spark运行在JVM中,所以要在Idea下运行spark,就先要安装JDK 1.8+ 然后加入S ...