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

后记

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

相关推荐
小柯J桑_几秒前
C++之特殊类设计
java·开发语言·c++
bobz9653 分钟前
bpftune
后端
QiZhang | UESTC6 分钟前
JAVA算法练习题day11
java·开发语言·python·算法·hot100
IT_陈寒6 分钟前
React 性能优化必杀技:这5个Hook组合让你的应用提速50%!
前端·人工智能·后端
FenceRain6 分钟前
spring boot 拦截器增加语言信息
java·spring boot·后端
bigdata-rookie8 分钟前
Java 反射
java·开发语言
能工智人小辰21 分钟前
Java8 Swing实现计算器
开发语言
SccTsAxR23 分钟前
[C语言]常见排序算法①
c语言·开发语言·经验分享·笔记·其他·排序算法
Monly2141 分钟前
Vue:下拉框多选影响行高
前端·javascript·vue.js
weixin_4365250743 分钟前
Spring Boot 集成 EasyExcel 的最佳实践:优雅实现 Excel 导入导出
java·spring boot·后端