Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

    • nvm

    • Node.js实例

    • Pm2

    • npm

    • 教程

      • Node.js-回调函数
      • Node.js-事件循环
      • Node.js-Buffer
      • Node.js-Stream
      • Node.js-函数
        • Node.js-模块系统
        • Node.js-路由
        • Node.js-全局对象
        • Node.js-常用工具
        • Node.js-文件系统
    • JavaScript优化

    • uniapp

    • Mini Program

    • TypeScript

    • 面向对象编程

    • UI组件

    • Plugin

    • Vue3

    • 性能优化

    • Axios

    • 状态管理

    • React

    • Mock

    • Icon

    • Template

    • 构建工具

    • 项目规范配置

    • Taro

    • SVG

    • React Native

    • 前端
    • Node.js
    • 教程
    HiuZing
    2023-02-17
    目录

    Node.js-函数

    # Node.js-函数

    在 JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数

    我们把 say 函数作为 execute 函数的第一个变量进行了传递

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

    function say(word) {
      console.log(word);
    }
    
    function execute(someFunction, value) {
      someFunction(value);
    }
    
    execute(say, "Hello");
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # 匿名函数

    我们可以把一个函数作为变量传递。但是我们不一定要绕这个"先定义,再传递"的圈子,我们可以直接在另一个函数的括号中定义和传递这个函数

    function execute(someFunction, value) {
      someFunction(value);
    }
    
    execute(function(word){ console.log(word) }, "Hello");
    
    1
    2
    3
    4
    5

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

    一般函数

    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
    2
    3
    4
    5
    6
    7
    8
    9

    向 createServer 函数传递了一个匿名函数

    var http = require("http");
    
    http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      response.write("Hello World");
      response.end();
    }).listen(8888);
    
    1
    2
    3
    4
    5
    6
    7
    上次更新: 2024/08/14, 04:14:33
    Node.js-Stream
    Node.js-模块系统

    ← Node.js-Stream Node.js-模块系统→

    最近更新
    01
    React Native 使用SVG
    08-13
    02
    Docker基础命令
    08-04
    03
    算数逻辑单元
    07-30
    更多文章>
    Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式