node函数使用

栏目: Node.js · 发布时间: 6年前

内容简介:在JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。Node.js中函数的使用与Javascript类似。以上代码中,我们把 say 函数作为execute函数的第一个变量进行了传递。这里传递的不是 say 的返回值,而是 say 本身!

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Design and Analysis of Distributed Algorithms (Wiley Series on P

Design and Analysis of Distributed Algorithms (Wiley Series on P

Nicola Santoro / Wiley-Interscience / 2006-10-27 / USD 140.95

This text is based on a simple and fully reactive computational model that allows for intuitive comprehension and logical designs. The principles and techniques presented can be applied to any distrib......一起来看看 《Design and Analysis of Distributed Algorithms (Wiley Series on P》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具