如题:在express
中使用路由,以下用伪代码描述:
路由:
app.use('/users', users);
路由处理:
var hello = require('./hello');
router.get('/', function(req, res, next) {
hello();
res.send('hello world');
});
hello.js
类方法
class Hello {
hello () {
console.log(this);
}
}
module.exports = new Hello().hello;
实际运行代码,发现 hello
方法中的this
为undefined
,这是为什么?
你这个Hello是个标准的class啊,一般里面的this是指向他的实例的,你直接new Hello().hello那么他的this自然就是undefined了