猿问

js中this指向的问题

这样写是可以正常调用的

router.route('/account')
.get(function (req, res, next) {
    User.find()
    .then(users => res.json(users))
    .catch(err => next(err));
});

我觉得这段代码可以简化成

router.route('/account')
.get(function (req, res, next) {
    User.find()
    .then(res.json)
    .catch(next);
});

可是采用第二种写法的时候在express的源码中会报错


就是在

var app = this.app;

这一行,debug发现this值是undefined,在js里面,对象的方法,this不是应该直接指向该对象吗?为什么这里this也就是res会是undefined呢?


梦里花落0921
浏览 569回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答