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

后记

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

相关推荐
w_zero_one34 分钟前
递归算法(一)
java·开发语言·算法
IT规划师41 分钟前
一文讲透五种发布机制:滚动、蓝绿、金丝雀、红黑与 AB
后端·面试
OpsEye1 小时前
大模型 Token 计量不准,是什么原因,怎么解决??
javascript·ai编程
AI 编程助手GPT1 小时前
curl 返回成功就能收文件?用本地 HTTP 验证错误页与超时
网络·网络协议·http
Julien20042 小时前
管理机密(一)
运维·服务器·开发语言·学习方法
Chen_harmony2 小时前
七、STL与string类
开发语言·c++
沐晓时光2 小时前
C语言入门,字符串和字符串函数知识讲解
c语言·开发语言
一条Go2 小时前
两套权限目录,三重闸门:Alioth 的多租户 RBAC 为什么这么拧
后端·程序员·github
xxxiugou1232 小时前
C++命名空间与引用全解析
开发语言·c++
Tokenfree2 小时前
把多模态 OCR 接进业务系统:四千张运单,三个坑
后端