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

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)—函数模块调用的更多相关文章
- NodeJS学习笔记 进阶 (12)Nodejs进阶:crypto模块之理论篇
个人总结:读完这篇文章需要30分钟,这篇文章讲解了使用Node处理加密算法的基础. 摘选自网络 Nodejs进阶:crypto模块之理论篇 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速 ...
- Nodejs进阶:核心模块net入门与实例讲解
模块概览 net模块是同样是nodejs的核心模块.在http模块概览里提到,http.Server继承了net.Server,此外,http客户端与http服务端的通信均依赖于socket(net. ...
- Nodejs进阶:核心模块https 之 如何优雅的访问12306
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严 ...
- Nodejs进阶:crypto模块中你需要掌握的安全基础
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址. 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速度增长.同时,各类网络安全问题层出不穷.在信 ...
- Nodejs进阶:核心模块Buffer常用API使用总结
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 Buffer是node的核心模块,开发者可以利用它来处 ...
- nodejs进阶(3)—路由处理
1. url.parse(url)解析 该方法将一个URL字符串转换成对象并返回. url.parse(urlStr, [parseQueryString], [slashesDenoteHost]) ...
- nodejs进阶(1)—输出hello world
下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var http = require ...
- NodeJS学习笔记 进阶 (13)Nodejs进阶:5分钟入门非对称加密用法
个人总结:读完这篇文章需要5分钟,这篇文章讲解了Node.js非对称加密算法的实现. 摘录自网络 地址: https://github.com/chyingp/nodejs-learning-guid ...
- Python3基础(4)匿名函数、装饰器、生成器、迭代器、内置函数、json&pickle序列化、软件目录开发规范、不同目录间模块调用
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
随机推荐
- ThreadLocal简单理解
在java开源项目的代码中看到一个类里ThreadLocal的属性: private static ThreadLocal<Boolean> clientMode = new Thread ...
- Web性能优化:What? Why? How?
为什么要提升web性能? Web性能黄金准则:只有10%~20%的最终用户响应时间花在了下载html文档上,其余的80%~90%时间花在了下载页面组件上. web性能对于用户体验有及其重要的影响,根据 ...
- 工欲善其事,必先利其器 之 VS2013全攻略(安装,技巧,快捷键,插件)!
如有需要WPF工具的朋友可以移步 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧 之前一篇<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATI ...
- ABP文档 - Mvc 视图
文档目录 本节内容: 简介 AbpWebViewPage 基类 简介 ABP通过nuget包Abp.Web.Mvc集成到Mvc视图里,你可以像往常那样创建常规的视图. AbpWebViewPage 基 ...
- rnandroid环境搭建
react-native 环境搭建具体步骤这个大家已经玩烂了,这个主要是记录下来自己做win7系统遇到的坑 1.com.android.ddmlib.installexception 遇到这个问题,在 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- potrace源码分析一
1 简介 potrace是由Dalhousie University的Peter Selinger开发一款位图轮廓矢量化软件,该软件源码是可以公开下载的,详细见项目主页:http://potrace. ...
- zookeeper源码分析之一服务端启动过程
zookeeper简介 zookeeper是为分布式应用提供分布式协作服务的开源软件.它提供了一组简单的原子操作,分布式应用可以基于这些原子操作来实现更高层次的同步服务,配置维护,组管理和命名.zoo ...
- PHP好用但又容易忽略的小知识
1.PHP函数之判断函数是否存在 当我们创建了自定义函数,并且了解了可变函数的用法,为了确保程序调用的函数是存在的,经常会先使用function_exists判断一下函数是否存在.同样的method_ ...
- .NET平台和C#编程的总结
第一章 简单认识.NET框架 (1)首先我们得知道 .NET框架具有两个主要组件:公共语言进行时CLR(Common Language Runtime)和框架类库FCL(Framework ...