node函数使用

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

内容简介:在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);

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

查看所有标签

猜你喜欢:

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

人工智能+:AI与IA如何重塑未来

人工智能+:AI与IA如何重塑未来

[美]韩德尔·琼斯(Handel Jones) [中]张臣雄 / 机械工业出版社 / 2018-10 / 55.00

当深度学习模型引发了全世界对人工智能的再次关注时,人工智能迎来第三次高速增长,人工智能(AI)、增强现实(AR)和虚拟现实(VR)正把人类带向新的“智能增强时代”(IA),我们将在不知不觉中接纳机器智能。 针对人类社会长期存在的众多复杂的动态的难题,人机融合智能将会提供全新的解决方案,谷歌、Facebook、微软、亚马逊、腾讯、阿里巴巴、百度等平台巨头纷纷斥千亿巨资布局人工智能的尖端技术;智......一起来看看 《人工智能+:AI与IA如何重塑未来》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具