私はjs + ES6 +クラスでちょっと新しいです。私はコンストラクタ内で関数を作成することに問題があります。あなたがいない私も元気です場合は、あまりにもそれに答えるようにしたい場合はJavascript、コンストラクタ関数を追加するクラス
#1. I need to add new Hobby, a person allowed to have plenty hobbies ;
#2. I don't know how to show all the data of students;
別の質問では場合に、comments
です。 これは私のコードです:
class Student {
constructor(name,hobbies){
this.name = name;
var hobby = new Set(); //do I set here or inside the function ??
//since the function addHobbies also need, then it's fine to be global right ?
this.hobbies = (hobbies) => { //function ES6 like this right ??
this.hobbies = hobby.add(hobbies);
return this.hobbies; //can I return hobby instead of this.hobbies ??
};
}
setName(newName){
this.name = newName;
}
addHobbies(newHobbies){
this.Hobbies = hobby.add(newHobbies); //it should be like this to add >> to set ?
}
getName(){
return this.name;
}
getHobbies(){
return this.hobbies;
}
}
どのようにすべてのデータを返すのですか?
let andy = new Student("andy","dance");
let vince = new Student("vince","codding");
だから、すべての学生属性がgetCode()
で表示されますか?
が 'それを行うには意味がありませんので、this.hobbies'は、単に、機能を返します返します。 'hobby.add(newHobbies);'は 'hobby'がそのスコープに存在しないので動作しません。 –
@FelixKlingよく、私はこれの前に趣味の機能を宣言しようとしました。 –