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

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

查看所有标签

猜你喜欢:

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

计算机程序设计艺术:第4卷 第4册(双语版)

计算机程序设计艺术:第4卷 第4册(双语版)

Donald E.Knuth / 苏运霖 / 机械工业出版社 / 2007-4 / 42.00元

关于算法分析的这多卷论著已经长期被公认为经典计算机科学的定义性描述。迄今已出版的完整的三卷组成了程序设计理论和实践的惟一的珍贵源泉,无数读者都赞扬Knuth的著作对个人的深远影响。科学家们为他的分析的美丽和优雅所惊叹,而从事实践的程序员们已经成功地应用他的“菜谱式”的解到日常问题上,所有人都由于Knuth在书中所表现出的博学、清晰、精确和高度幽默而对他无比敬仰。   为开始后续各卷的写作并更......一起来看看 《计算机程序设计艺术:第4卷 第4册(双语版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

RGB CMYK 互转工具