小编Ast*_*nca的帖子

如果在Express js中间件中调用函数表达式,为什么会悬挂呢?

我们知道函数声明已被提升,您可以在脚本中的任何位置调用它们。函数表达式不是这种情况。

例如:

 test();

 const test = () => {
    console.log(1+3);
 }

 When we call test() it will always return undefined.
Run Code Online (Sandbox Code Playgroud)

但是,当我们在expressjs中间件中调用相同的函数时,不会发生这种情况。

router.get('/', (req, res, next) => {
   test(); // it will return always the result 4
})

const test = () => {
   console.log(1+3);
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么会这样吗?

javascript node.js express

2
推荐指数
1
解决办法
48
查看次数

标签 统计

express ×1

javascript ×1

node.js ×1