将数组 属性 添加到此 Express.js 模型 class
Add an array property to this Express.js model class
需要对以下代码进行哪些具体更改才能成功将 属性 添加到 JavaScript 模型对象中 JavaScript =25=] app? AppUser
代码放在 /app/models
directory of this GitHub link.
的新 appuser.js
文件中
这是 AppUser
class 的代码,包括数组的 getter 和 setter 的占位符,此 OP 询问如何编写:
var method = AppUser.prototype;
function AppUser(name) {
this._name = name;
}
method.getName = function() {
return this._name;
};
//scopes
method.getScopes = function() {
//return the array of scope string values
};
method.setScopes = function(scopes) {
//set the new scopes array to be the scopes array for the AppUser instance
//if the AppUser instance already has a scopes array, delete it first
};
method.addScope = function(scope) {
//check to see if the value is already in the array
//if not, then add new scope value to array
};
method.removeScope = function(scope) {
//loop through array, and remove the value when it is found
}
module.exports = AppUser;
你可以像这样在 ES6 中使用 class :
'use strict';
module.exports = class AppUser {
constructor(name) {
this.name = name;
this.scopes = [];
}
getName() {
return this.name;
}
getScopes() {
return this.scopes;
}
addScope(scope){
if (this.scopes.indexOf(scope) === -1) this.scopes.push(scope);
}
}
需要对以下代码进行哪些具体更改才能成功将 属性 添加到 JavaScript 模型对象中 JavaScript =25=] app? AppUser
代码放在 /app/models
directory of this GitHub link.
appuser.js
文件中
这是 AppUser
class 的代码,包括数组的 getter 和 setter 的占位符,此 OP 询问如何编写:
var method = AppUser.prototype;
function AppUser(name) {
this._name = name;
}
method.getName = function() {
return this._name;
};
//scopes
method.getScopes = function() {
//return the array of scope string values
};
method.setScopes = function(scopes) {
//set the new scopes array to be the scopes array for the AppUser instance
//if the AppUser instance already has a scopes array, delete it first
};
method.addScope = function(scope) {
//check to see if the value is already in the array
//if not, then add new scope value to array
};
method.removeScope = function(scope) {
//loop through array, and remove the value when it is found
}
module.exports = AppUser;
你可以像这样在 ES6 中使用 class :
'use strict';
module.exports = class AppUser {
constructor(name) {
this.name = name;
this.scopes = [];
}
getName() {
return this.name;
}
getScopes() {
return this.scopes;
}
addScope(scope){
if (this.scopes.indexOf(scope) === -1) this.scopes.push(scope);
}
}