zoukankan      html  css  js  c++  java
  • Node js函数

    /*
    在JavaScript中,一个函数可以作为另一个函数的参数,函数名可做变量名。可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。
    把函数做参数一般是主要用于回调函数,即被调用的方法执行完后调用作为参数的方法
    Node.js中函数的使用与Javascript类似
    例
    */
    //===============================以下代码为main.js文件中内容==========================================
    function say(word) {
        console.log(word);
    }
    
    function execute(someFunction, value) {
        someFunction(value);
    }
    execute(say, "Hello");
    //===============================以上代码为main.js文件中内容==========================================
    /*
    以上代码,把 say 函数作为execute函数的第一个变量进行了传递。这里传递的不是 say 的返回值,而是 say 本身
    这样一来, say 就变成了execute 中的本地变量 someFunction ,execute可以通过调用 someFunction() (带括号的形式)来使用 say 函数。
    因为 say 有一个变量, execute 在调用 someFunction 时可以传递这样一个变量,也可不传,但如果say中用到该参数会报undefined。
    
    执行main.js
    node main.js
    输出内容:
    Hello
    */
    
    
    
    
    
    
    
    
    /*
    匿名函数
    可以把一个函数作为变量传递。但是不一定要绕这个"先定义,再传递"的圈子,可以直接在另一个函数的括号中定义和传递这个函数
    在 execute 接受第一个参数的地方直接定义了准备传递给 execute 的函数。
    用这种方式,甚至不用给这个函数起名字,这也是为什么它被叫做匿名函数 。
    例
    */
    //===============================以下代码为main.js文件中内容==========================================
    function execute(someFunction, value) {
        someFunction(value);
    }
    execute(function(word){ console.log(word) }, "Hello");
    //===============================以上代码为main.js文件中内容==========================================
    /*
    执行main.js
    node main.js
    输出内容:
    Hello
    */
    
    
    
    
    
    
    
    
    //带着这些知识,再来看看简约而不简单的HTTP服务器
    //现在它看上去应该清晰了很多:向 createServer 函数传递了一个匿名函数。
    //===============================以下代码为main.js文件中内容==========================================
    var http = require("http");
    
    http.createServer(function(request, response) {
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }).listen(8888);
    //===============================以上代码为main.js文件中内容==========================================
    /*
    执行main.js
    node main.js
    在浏览器中输入 http://127.0.0.1:8888/
    页面显示
    Hello World
    */
    
    
    
    
    
    
    
    /*
    也可以用如下写法,效果相同
    */
    //===============================以下代码为main.js文件中内容==========================================
    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);
    //===============================以上代码为main.js文件中内容==========================================
  • 相关阅读:
    DGA域名可以是色情网站域名
    使用cloudflare加速你的网站隐藏你的网站IP
    167. Two Sum II
    leetcode 563. Binary Tree Tilt
    python 多线程
    leetcode 404. Sum of Left Leaves
    leetcode 100. Same Tree
    leetcode 383. Ransom Note
    leetcode 122. Best Time to Buy and Sell Stock II
    天津Uber优步司机奖励政策(12月28日到12月29日)
  • 原文地址:https://www.cnblogs.com/dreamhome/p/8635621.html
Copyright © 2011-2022 走看看