node.js(十二)

作者: yyshang | 来源:发表于2017-05-11 15:30 被阅读18次

Node.js 函数
在JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。
Node.js中函数的使用与Javascript类似,举例来说,你可以这样做:

function say(word) {
  console.log(word);
}

function execute(someFunction, value) {
  someFunction(value);
}

execute(say, "Hello");

以上代码中,我们把 say 函数作为execute函数的第一个变量进行了传递。这里返回的不是 say 的返回值,而是 say 本身!
这样一来, say 就变成了execute 中的本地变量 someFunction ,execute可以通过调用 someFunction() (带括号的形式)来使用 say 函数。
当然,因为 say 有一个变量, execute 在调用 someFunction 时可以传递这样一个变量。
匿名函数
我们可以把一个函数作为变量传递。但是我们不一定要绕这个"先定义,再传递"的圈子,我们可以直接在另一个函数的括号中定义和传递这个函数:

function execute(someFunction, value) {
  someFunction(value);
}

execute(function(word){ console.log(word) }, "Hello");

我们在 execute 接受第一个参数的地方直接定义了我们准备传递给 execute 的函数。
用这种方式,我们甚至不用给这个函数起名字,这也是为什么它被叫做匿名函数 。
函数传递是如何让HTTP服务器工作的
带着这些知识,我们再来看看我们简约而不简单的HTTP服务器:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

现在它看上去应该清晰了很多:我们向 createServer 函数传递了一个匿名函数。
用这样的代码也可以达到同样的目的:

var http = require("http");

function onRequest(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}

http.createServer(onRequest).listen(8888);

相关文章

  • node.js(十二)

    Node.js 函数在JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,...

  • nodejs安装

    Node.js安装 目录 Node.js简单介绍 windows安装Node.js Linux安装Node.js ...

  • node.js基础

    什么是node.js Node.js特点 node.js优点和缺点

  • Nodejs.2

    参考内容:Node.js EventEmitter 四、Node.js EventEmitter Node.js所...

  • nodejs第一步

    Node.js 是什么?Node.js与JavaScript的区别是什么? Node.js的优点?Node.js的...

  • node 学习笔记.md

    Node.js第一天 1. 初识Node.js 1.1 Node.js是什么 Node.js® is a Java...

  • Node.js学习

    主线:Node.js是什么 --> Node.js的组成 --> Node.js的特点 --> Helloworl...

  • 使用AngularJS搭建前台框架

    Node.js部署: 下载安装包:从Node.js官网下载Node.js安装包。 安装Node.js:打开node...

  • Node.js模块

    Node.js 模块和 Node.js 包介绍。 一、Node.js模块 每一个Node.js都是一个Node.j...

  • 小鹅通视频下载mac 小鹅通课程下载教程

    前两天,Node.js官方发布了Node.js 15的正式版本,Node.js 15 将替代 Node.js 14...

网友评论

    本文标题:node.js(十二)

    本文链接:https://www.haomeiwen.com/subject/rxbotxtx.html