简体   繁体   English

此javascript es6胖箭头函数的db参数的值或用法是什么?

[英]what is the db argument's value or usage for this javascript es6 fat arrow function?

initializeDb( db => {

    // internal middleware
    app.use(middleware({ config, db }));

    // api router
    app.use('/api', api({ config, db }));

    app.server.listen(process.env.PORT || config.port, () => {
        console.log(`Started on port ${app.server.address().port}`);
    });
});

https://github.com/developit/express-es6-rest-api/blob/master/src/index.js#L27 https://github.com/developit/express-es6-rest-api/blob/master/src/index.js#L27

Above situation, the db seems pop out of nowhere, to see the context, please click on the link direct to the source code. 在上述情况下,数据库似乎突然冒出来,要查看上下文,请单击直接指向源代码的链接。

I simulated the same way here ( https://codepen.io/adamchenwei/pen/vWWmXa ) the db shouldn't even be accessible. 我在此处( https://codepen.io/adamchenwei/pen/vWWmXa )以相同的方式进行了模拟,甚至无法访问该db What exactly the db does then in that specific boilerplate? 在那个特定的样板里, db究竟做了什么?

db is the argument of the call back function of initializeDb dbinitializeDb回调函数的initializeDb

The code actually looks like this 该代码实际上看起来像这样

'use strict';

initializeDb(function (db) {

    // internal middleware
    app.use(middleware({ config: config, db: db }));

    // api router
    app.use('/api', api({ config: config, db: db }));

    app.server.listen(process.env.PORT || config.port, function () {
        console.log('Started on port ' + app.server.address().port);
    });
});

Above situation, the db seems pop out of nowhere 以上情况,数据库似乎突然出现

It's the parameter of the function you pass to initializeDb . 这是您传递给initializeDb的函数的参数。 initializeDb presumable creates a new database instance and passes that instance to the the function you pass to it. initializeDb presumable创建一个新的数据库实例,并将该实例传递给您传递给它的函数。

That's just how functions work, they accept parameters that are provided at call time. 这就是函数的工作方式,它们接受在调用时提供的参数。 Only in your example it is not you who calls the function, but another function ( initializeDb ). 仅在您的示例中,不是调用函数,而是另一个函数( initializeDb )。

Simplified example of a function that takes a callback: 接受回调的函数的简化示例:

 function answerToEverything(callback) { callback(42); } answerToEverything(answer => console.log(answer)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM