我有點新的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);'不適用,因爲'愛好'不存在於該範圍內。 –
@FelixKling以及我在此之前嘗試聲明愛好功能,沒有像我想要的那樣工作。 –