IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

十三、Node.js 函数

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=5442

JavaScript 中的函数是一等公民,一个函数可以作为另一个函数的参数。

我们既可以先定义一个函数,然后作为参数传递给另一个函数,也可以直接在传递参数的地方定义一个匿名函数

因为 Node.js 基于 JavaScript ,所以在 Node.js 中定义和使用函数和 JavaScript 中一样

/*
 * filename: main.js
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/

function say(word) {
  console.log(word);
}

function execute(someFunction, value) {
  someFunction(value);
}

execute(say, "Hello");

这段代码,我们把 say 函数作为 execute 函数的第一个参数传递

此时,say 函数返回的不是它自己的返回值,而是返回 say 函数本身

这样一来,say 就变成了 execute 中的本地变量 someFunction ,execute 可以通过调用 someFunction() (带括号的形式)来使用 say 函数

因为 say 函数需要一个参数, execute 在调用 someFunction 时可以传递这样一个参数

运行以上 Node.js 范例,输出结果如下

$ node main.js
Hello

匿名函数

JavaScript 将函数作为参数进行传递,不一定要遵循 “先定义,再传递” 的原则,可以直接在传递的时候定义函数,这种函数我们称之为 匿名函数

用这种方式,我们甚至不用这种参数函数起名字,这也是为什么它被叫做匿名函数

使用匿名函数,我们可以将上面的范例改写如下

/*
 * filename: main.js
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/

function execute(someFunction, value) {
  someFunction(value);
}

execute(function(word){ console.log(word) }, "Hello");

我们在 execute 接受第一个参数的地方直接定义了传递给 execute 的函数

运行以上 Node.js 脚本,输出结果如下

$ node main.js
Hello

函数参数传递是如何让 HTTP 服务工作的

使用函数作为参数,我们可以把本来很复杂的逻辑整理的清楚明白

/*
 * filename: main.js
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

这段代码使用了匿名函数,而且已经够清楚明白了

当然,我们也可以把上面的代码改写成如下的格式

/*
 * filename: main.js
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/

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

干货推荐

本站推荐:精选优质专栏

附录:Node.js 教程:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(80) 打赏



未经允许不得转载:搜云库技术团队 » 十三、Node.js 函数

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367