1.Node.js-函数和匿名函数的用法

题记

函数和匿名函数的简单用法

定义函数

定义普通函数

function 函数名(参数) {

// 函数体

}

定义参数为函数的函数

可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数

function say(word) {

console.log(word);

}

function execute(someFunction, value) {

someFunction(value);

}

execute(say, "Hello");

定义匿名函数

直接把函数写在参数的位置,不用给函数写名字

function execute(someFunction, value) {

someFunction(value);

}

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

创建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);

使用普通函数的形式:

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

后记

觉得有用可以点赞或收藏!

相关推荐
广州山泉婚姻2 分钟前
C语言三种基本程序结构详解
c语言·开发语言
Ruci ALYS5 分钟前
SpringBoot Maven快速上手
spring boot·后端·maven
上弦月-编程6 分钟前
【C语言】函数栈帧的创建与销毁(底层原理)
c语言·开发语言
java1234_小锋8 分钟前
谈谈Ribbon和Feign区别?
后端·spring cloud·ribbon
eqwaak09 分钟前
PyTorch张量操作全攻略:从入门到精通
开发语言·人工智能·pytorch·python
傻瓜搬砖人9 分钟前
SpringMVC的请求
java·前端·javascript·spring
辞旧 lekkk10 分钟前
【Qt】初识(上)
开发语言·数据库·qt·学习·萌新
格林威12 分钟前
线阵工业相机:如何计算线阵相机的行频(Line Rate)?公式+实例
开发语言·人工智能·数码相机·算法·计算机视觉·工业相机·线阵相机
Chasing Aurora13 分钟前
python 安装依赖和导入模块 详解
开发语言·python·虚拟环境·import·pyenv·requirements
近津薪荼16 分钟前
C++ vector容器底层深度剖析与模拟实现
开发语言·c++