gpt4 book ai didi

javascript - 调用不带参数且未定义默认参数的函数

转载 作者:行者123 更新时间:2023-12-02 18:28:21 24 4
gpt4 key购买 nike

来自示例here :

app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});

function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}

我不明白 ensureAuthenticated 是如何工作的。它需要 3 个参数,没有设置默认参数。但如果我不带参数调用它(在 app.get 中),它确实会正确执行,这怎么可能?

最佳答案

您实际上并没有在代码中的任何地方调用ensureAuthenticated;您正在传递对函数的引用,并且您的http框架稍后调用该函数(当向/account发出请求时)正确的论据。

如果您编写了 ensureAuthenticated() (带括号),那么您将在不带参数的情况下调用它。如果没有括号,您将传递对该函数的引用。

关于javascript - 调用不带参数且未定义默认参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18115974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com