2016-07-08 2 views
2

Express.jsアプリケーションのJavaScriptモデルオブジェクトに文字列の配列であるプロパティを正常に追加するには、以下のコードを変更する必要があります?AppUserコードは、/app/models directory of this GitHub linkの新しいappuser.jsファイルに格納されています。ここでこのExpress.jsモデルクラスに配列プロパティを追加

はこのOPは書く方法を尋ねる配列、のためのゲッターとセッターのためのプレースホルダを含むAppUserクラスのコードです:

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; 

答えて

2

あなたはこのようES6でクラスを使用することができます。

'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); 
    } 
} 
+0

コーディングレベルを向上させたい場合は、試してみる必要があります。それでも問題が解決しない場合は、コードを投稿してください。さもなければ、あなたは宿題をしたいと思うかもしれません。 –

+0

「厳密に使う」ことを忘れないでください。それは私のplateform Express "version": "4.13.3"で動作します。しかし、それはどのようにこのクラスを明示的に使用するかによって決まります... –

+0

は()のために修正されました。しかし、 "厳密な使用"または "厳格な使用"は同じです –

関連する問題