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

Node.js中函数的使用与Javascript类似。

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函数

1.不带参的函数    function sayhello(){            console.log('Hello World');        }        sayhello()        //运行结果  Hello World    2.带参的函数    function sayyouwrite(youwrite){            console.log(youwrite);        }        sayyouwrite('你好')        //运行结果  你好    3.多个参数函数    function sayyouwrite2(youwrite1,youwrite2,youwrite3){            console.log(youwrite1+youwrite2+youwrite3);            console.log(youwrite1);            console.log(youwrite2);            console.log(youwrite3);        }        sayyouwrite('你好')        // 运行结果        // 你好!世界!中国!        // 你好!        // 世界!        // 中国!    4.匿名函数    function execute(someFunc, value) {            someFunc(value)        }                 execute(function (world) {            console.log(world)        }, "Hello world")

函数的调用

1.js文件内部函数调用    var http = require('http')        http.createServer(function (request, response) {            // 发送 HTTP 头部            // HTTP 状态值: 200 : OK            // 内容类型: text/plain            response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});            if(request.url="/favicon.ico"){                fun1(response);                response.end('')            }        }).listen(8888);                function fun1(res) {            console.log("我是fun1")            res.write("你好,我是fun1|")        }        // 终端打印如下信息        console.log('Server running at http://127.0.0.1:8888/');    2.调用其他js文件内的函数    var http = require('http')        var fun2 = require("./m2.js")        http.createServer(function (request, response) {            // 发送 HTTP 头部            // HTTP 状态值: 200 : OK            // 内容类型: text/plain            response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});            if(request.url="/favicon.ico"){                fun1(response);                fun2(response);                response.end('')            }        }).listen(8888);        function fun1(res) {            console.log("我是fun1")            res.write("你好,我是fun1|")        }        // 终端打印如下信息        console.log('Server running at http://127.0.0.1:8888/');        m2.js:        function fun2(res) {            console.log("我是fun2")            res.write("你好,我是fun2")        }        module.exports = fun2;//只能引用一个函数    3.调用其他js文件中多个函数    var http = require('http')        var funx = require("./m2.js")        http.createServer(function (request, response) {            // 发送 HTTP 头部            // HTTP 状态值: 200 : OK            // 内容类型: text/plain            response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});            if(request.url="/favicon.ico"){                fun1(response);                funx.fun2(response); // funx.fun2(response);                funx.fun3(response);                response.end('')            }        }).listen(8888);        function fun1(res) {            console.log("我是fun1")            res.write("你好,我是fun1|")        }        // 终端打印如下信息        console.log('Server running at http://127.0.0.1:8888/');        m2.js            module.exports ={            fun2:function (res) {                console.log("我是fun2")                res.write("你好,我是fun2|")            },            fun3:function (res) {                console.log("我是fun3")                res.write("你好,我是fun3")            }        }    

同时我们也可以将m1.js文件里面的

        

funx.fun2(response);        funx.fun3(response);    替换为        funx['fun2'](response);        funx['fun3'](response);    或        fname2 = 'fun2';        fname3 = 'fun3';        funx[fname2](response);        funx[fname3](response);

函数传递是如何让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);