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

后记

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

相关推荐
沐森11 分钟前
面向 Trait 编程 (Trait-Driven Design)
后端
深蓝电商API11 分钟前
企业级爬虫架构设计:任务调度、容错、重试、降重
开发语言·爬虫·ruby
韭菜炒大葱13 分钟前
React 新手村通关指南:状态、组件与魔法 UI 🧙‍♂️
前端·javascript·react.js
芝麻开门-新起点17 分钟前
第13-1章 Python地理空间开发
开发语言·python
Dwzun25 分钟前
基于SpringBoot+Vue的二手书籍交易平台系统【附源码+文档+部署视频+讲解)
java·vue.js·spring boot·后端·spring·计算机毕业设计
期待のcode29 分钟前
Wrapper体系中的condition参数
java·spring boot·后端·mybatis
肥大毛39 分钟前
C++入门学习---结构体
开发语言·c++·学习
小明记账簿40 分钟前
JavaScript浮点数精度问题及解决方案
开发语言·javascript·ecmascript
雨中飘荡的记忆1 小时前
Spring状态机深度解析
java·后端·spring
南棱笑笑生1 小时前
20251213给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时适配type-C0
linux·c语言·开发语言·rockchip