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

后记

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

相关推荐
Tony Bai7 分钟前
【Go模块构建与依赖管理】09 企业级实践:私有仓库与私有 Proxy
开发语言·后端·golang
qq. 280403398416 分钟前
vue介绍
前端·javascript·vue.js
Lucky小小吴17 分钟前
开源项目5——Go版本快速管理工具
开发语言·golang·开源
Mr.Jessy29 分钟前
Web APIs 学习第五天:日期对象与DOM节点
开发语言·前端·javascript·学习·html
杨福瑞34 分钟前
数据结构:单链表(2)
c语言·开发语言·数据结构
进化中的码农35 分钟前
Go中的泛型编程和reflect(反射)
开发语言·笔记·golang
咖啡教室1 小时前
每日一个计算机小知识:ICMP
后端·网络协议
间彧1 小时前
OpenStack在混合云架构中通常扮演什么角色?
后端
咖啡教室1 小时前
每日一个计算机小知识:IGMP
后端·网络协议
间彧1 小时前
云原生技术栈中的核心组件(如Kubernetes、Docker)具体是如何协同工作的?
后端