如何修复下面的代码,以便能够使用call调用类方法。
类别定义:
class User {
constructor(..) {...}
async method(start, end) {}
}试图将类方法作为函数参数传递:
const User = require('./user');
async function getData(req, res) {
// User.method is undefined, since User refers to User constructor
await get(req, res, User.method);
}
async function get(req, res, f) {
let start = ...;
let end = ...;
let params = ...;
let user = new User(params);
// f is undefined here
let stream = await f.call(user, start, end);
}发布于 2015-08-29 12:27:16
方法是未定义的,因为用户引用构造函数。
你在找User.prototype.method
async function getData(req, res) {
await get(req, res, User.prototype.method);
}请记住,ES6类是语言的原型特性之上的语法糖。
https://stackoverflow.com/questions/32285999
复制相似问题