【发布时间】:2019-01-27 02:08:14
【问题描述】:
我在 NodeJS 中有一个 Babel ES5 类设置,例如:
import fs from "fs";
// Services
import { aws } from "../../services/aws";
class UserController {
update(req, res, next) {
const { user } = req.body;
if (user) {
req.user = Object.assign(req.user, user);
req.user.save((err, updatedUser) => {
if (err) {
return res.status(422).json(err);
}
return res.json({ user: updatedUser });
});
} else {
return res.sendStatus(400);
}
}
testMethod() {
this.update();
}
}
module.exports = new UserController();
如何从另一个父方法中访问“更新”方法?在这种情况下很难看出“this”是如何定义的
【问题讨论】:
-
这取决于 testMethod 的使用方式。这是 ES6,不是 ES5。
标签: node.js babeljs ecmascript-5