尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

HoRain云--Node.js 函数

HoRain云--Node.js 函数 函数是一段可重复使用的代码块用于执行特定任务。在 Node.js 中函数是 JavaScript 的核心组成部分之一是构建应用程序的基本单元。Node.js 继承了 JavaScript 的所有函数特性并在其异步编程模型中发挥了重要作用。在 JavaScript中一个函数可以作为另一个函数的参数。我们可以先定义一个函数然后传递也可以在传递参数的地方直接定义函数。函数的重要性代码复用避免重复编写相同逻辑模块化将复杂问题分解为小函数可维护性便于调试和修改独立功能单元函数声明使用 function 关键字声明一个函数。function greet(name) { console.log(Hello, ${name}!); }函数表达式将函数赋值给一个变量。const greet function(name) { console.log(Hello, ${name}!); };箭头函数ES6 引入的简洁函数表达式。const greet (name) { console.log(Hello, ${name}!); }; // 单行箭头函数 const greet name console.log(Hello, ${name}!);函数的类型1、普通函数最常见的函数形式可以有参数和返回值。function add(a, b) { return a b; }2、匿名函数没有名字的函数通常作为参数传递给其他函数。setTimeout(function() { console.log(This is an anonymous function.); }, 1000);3、回调函数作为参数传递给另一个函数并在某个操作完成后被调用。function fetchData(callback) { setTimeout(() { const data Some data; callback(data); }, 1000); } fetchData((data) { console.log(data); });异步函数从回调到 Promise。实例function readFilePromise(path) {return new Promise((resolve, reject) {fs.readFile(path, utf8, (err, data) {if (err) reject(err);else resolve(data);});});}使用 async 和 await 关键字处理异步操作。实例async function fetchUser(id) {try {const response await fetch(https://api.example.com/users/${id});const user await response.json();return user;} catch (error) {console.error(Error fetching user:, error);}}fetchUser(1).then(user console.log(user));调用方式对比调用方式示例特点直接调用greet(Alice)最常见的方式作为方法调用obj.method()this 指向调用对象构造函数调用new Constructor()创建新实例间接调用greet.call(null, Bob)可改变 this 指向高级用法1、闭包闭包是指一个函数能够记住并访问其词法作用域即使这个函数在其词法作用域之外执行。实例function createCounter() {let count 0;return function() {count;return count;};}const counter createCounter();console.log(counter()); // 1console.log(counter()); // 22、高阶函数接受函数作为参数或返回函数的函数。实例function applyOperation(a, b, operation) {return operation(a, b);}const sum applyOperation(5, 3, (x, y) x y);const product applyOperation(5, 3, (x, y) x * y);console.log(sum); // 8console.log(product); // 15函数参数处理默认参数在函数声明时为参数提供默认值。function greet(name Guest) { console.log(Hello, ${name}!); } greet(); // Hello, Guest! greet(Alice); // Hello, Alice!剩余参数允许将不定数量的参数表示为一个数组。实例function sum(...numbers) {return numbers.reduce((acc, num) acc num, 0);}console.log(sum(1, 2, 3, 4)); // 10解构参数从对象或数组中提取数据并将其赋值给变量。实例function getUserInfo({ name, age }) {console.log(Name: ${name}, Age: ${age});}const user { name: Alice, age: 30 };getUserInfo(user); // Name: Alice, Age: 30实例函数单一职责每个函数应该只做一件事这样更容易测试和维护。实例function calculateArea(width, height) {return width * height;}避免全局变量尽量减少全局变量的使用使用局部变量和函数参数。实例function calculateTotal(items) {let total 0;for (const item of items) {total item.price * item.quantity;}return total;}使用箭头函数箭头函数简洁明了特别是在处理回调函数时。实例const users [{ id: 1, name: Alice },{ id: 2, name: Bob }];const names users.map(user user.name);console.log(names); // [Alice, Bob]错误处理使用 try...catch 语句处理可能抛出的错误。实例async function fetchUser(id) {try {const response await fetch(https://api.example.com/users/${id});if (!response.ok) {throw new Error(Network response was not ok);}const user await response.json();return user;} catch (error) {console.error(Error fetching user:, error);}}函数作为参数:函数作为一个参数来使用。实例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 时可以传递这样一个变量。匿名函数我们可以把一个函数作为变量传递。但是我们不一定要绕这个先定义再传递的圈子我们可以直接在另一个函数的括号中定义和传递这个函数实例function execute(someFunction, value) {someFunction(value);}execute(function(word){ console.log(word) }, Hello);我们在 execute 接受第一个参数的地方直接定义了我们准备传递给 execute 的函数。用这种方式我们甚至不用给这个函数起名字这也是为什么它被叫做匿名函数 。函数传递是如何让 HTTP 服务器工作的带着这些知识我们再来看看我们简约而不简单的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);现在它看上去应该清晰了很多我们向 createServer 函数传递了一个匿名函数。用这样的代码也可以达到同样的目的实例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);函数最佳实践函数设计原则单一职责一个函数只做一件事合理命名使用动词名词形式如getUserInfo控制长度建议不超过20行代码避免副作用纯函数更易于测试和维护性能优化技巧实例// 使用闭包缓存结果function memoize(fn) {const cache new Map();return (...args) {const key JSON.stringify(args);if (cache.has(key)) return cache.get(key);const result fn(...args);cache.set(key, result);return result;};}实战练习练习1创建温度转换函数实例/*** 实现摄氏度和华氏度互相转换* param {number} temp - 温度值* param {string} unit - 原始单位 (C 或 F)* returns {number} 转换后的温度*/function convertTemperature(temp, unit) {// 你的代码...}练习2实现简单的事件发射器实例class EventEmitter {constructor() {this.events {};}// 实现 on/emit/off 方法}
返回列表