我正在尝试创建一个用户模型,但是我得到了:
2018-12-18 09:47:07.567 [错误]:uncaughtException:无法设置 未定义TypeError的属性'getUserId':无法设置属性 未定义的“ getUserId”
这是模型:
var User = (userId, name, lastname) => {
this.userId = userId;
this.name = name;
this.lastname = lastname;
this.tokenizeSingle = null;
this.tokenizeFile = null;
this.detokenizeSingle = null;
this.detokenizeFile = null;
}
User.prototype.getUserId = () => {
return this.userId;
}
User.prototype.getName = () => {
return this.name;
}
User.prototype.getLastname = () => {
return this.name;
}
User.prototype.getFullName = () => {
return this.name + ' ' + this.lastname;
}
User.prototype.getTokenizeSingle = () => {
return this.tokenizeSingle;
}
User.prototype.getTokenizeFile = () => {
return this.tokenizeFile;
}
User.prototype.getDetokenizeSingle = () => {
return this.detokenizeSingle;
}
User.prototype.getDetokenizeFile = () => {
return this.detokenizeFile;
}
User.prototype.setPermissions = (tokenizeSingle, tokenizeFile, detokenizeSingle, detokenizeFile) => {
this.tokenizeSingle = tokenizeSingle || null;
this.tokenizeFile = tokenizeFile || null;
this.detokenizeSingle = detokenizeSingle || null;
this.detokenizeFile = detokenizeFile || null;
}
module.exports = User;
我也在使用VS Code,并且这样做时没有任何建议:var newUser = new User(1, 'Rand', 'Fisk');
在newUser
中加点并没有显示原型中设置的任何选项。
我想念什么?
答案 0 :(得分:3)
箭头函数没有自己的this
上下文。更改为常规功能,它应该可以工作:
function User(userId, name, lastname) {
this.userId = userId;
this.name = name;
this.lastname = lastname;
this.tokenizeSingle = null;
this.tokenizeFile = null;
this.detokenizeSingle = null;
this.detokenizeFile = null;
}
User.prototype.getUserId = function() {
return this.userId;
}