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

后记

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

相关推荐
游乐码1 分钟前
C#List
开发语言·c#·list
淡笑沐白4 分钟前
ECharts入门指南:数据可视化实战
前端·javascript·echarts
xyq20246 分钟前
jQuery Tooltip:深入解析与最佳实践
开发语言
夜猫子ing7 分钟前
如何编写一个CMakelists文件
开发语言·c++
开发者如是说7 分钟前
可能是最好用的多语言管理工具
android·前端·后端
非科班Java出身GISer9 分钟前
ArcGIS JS 基础教程(1):地图初始化(含AMD/ESM两种引入方式)
javascript·arcgis·arcgis js·arcgis js 初始化·arcgis js 地图初始化
Ivanqhz17 分钟前
LLVM IR 转 SMT公式
java·开发语言
小红的布丁22 分钟前
Reactor 模型详解:单 Reactor、主从 Reactor 与 Netty 思想
android·java·开发语言
被摘下的星星30 分钟前
Java的类加载
java·开发语言
skilllite作者33 分钟前
SkillLite 多入口架构实战:CLI / Python SDK / MCP / Desktop / Swarm 一页理清
开发语言·人工智能·python·安全·架构·rust·agentskills