2016-12-07 99 views
0

我有點新的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()屬性?

+0

'返回this.hobbies'將直接返回功能,這樣沒有意義的事情。 'hobby.add(newHobbies);'不適用,因爲'愛好'不存在於該範圍內。 –

+0

@FelixKling以及我在此之前嘗試聲明愛好功能,沒有像我想要的那樣工作。 –

回答

1

試試這個:

class Student { 
    constructor(name, hobbies) { 
    this.name = name; 

    // Allow passing both an array of hobbies and a single hobby 
    this.hobbies = Array.isArray(hobbies) ? new Set(hobbies) : new Set([hobbies]); 
    } 

    setName(newName) { 
    this.name = newName; 
    } 

    addHobbies(newHobbies) { 
     if (Array.isArray(newHobbies)) { 
      newHobbies.forEach((hobby) => this.hobbies.add(hobby)); 
     } else { 
      this.hobbies.add(newHobbies); 
     } 
    } 

    getName() { 
    return this.name; 
    } 

    getHobbies() { 
    return this.hobbies; 
    } 
} 

let andy = new Student("andy","dancing"); 
let vince = new Student("vince",["codding", "running"]); 
andy.addHobbies("slipping"); 
vince.addHobbies(["running", "eating"]); 
+0

這是JavaScript。沒有'private'關鍵字或任何成員聲明。 – Bergi

+0

只有一件事情是''this.hobbies'有一種類型更好。我的意思是在構造函數中,你必須創建一個元素的數組,甚至是一個Set來讓它們獨一無二 – ZedXter

+0

@Bergi你是對的,我自己使用TypeScript並從那裏接受它。我修改了我的答案。謝謝! – Shai

1

我在這裏設置或裏面的功能?

這取決於你需要什麼。你是否想讓每個Student代替一組業餘愛好,還是每次調用該函數時都想創建一個新集?

this.hobbies = (hobbies) => { //function ES6 like this right ?? 
    this.hobbies = hobby.add(hobbies); 

這並不在所有的工作。您正在使用函數值創建屬性,但是當調用該方法時,您將覆蓋屬性,返回值爲add method


要使其工作,我建議你做的.hobbies設置instance property instead of a local variable

class Student { 
    constructor(name, ...hobbies) { 
    this.name = name; 
    this.hobbies = new Set(); 
    this.addHobbies(...hobbies); 
    } 

    getName() { 
    return this.name; 
    } 
    setName(newName) { 
    this.name = newName; 
    } 

    getHobbies() { 
    return this.hobbies; 
    } 
    addHobbies(...newHobbies) { 
    for (const newHobby of newHobbies) 
     this.hobbies.add(newHobby); 
    } 
} 

或者,如果你堅持使用本地構造變量,它應該是這樣的:

class Student { 
    constructor(name, ...hobbies) { 
    this.name = name; 
    this.hobbies = new Set(...hobbies); 

    this.getHobbies =() => { 
     return this.hobbies; 
    }; 
    this.addHobbies = (...newHobbies) => { 
     for (const newHobby of newHobbies) 
     this.hobbies.add(newHobby); 
    }; 
    } 

    … // further methods (for name etc) 
} 
0

你是在正確的方向。我已經重寫了你的班級,去做我認爲更接近你想要達到的目標的班級。

播放與代碼:https://jsbin.com/vejumo/edit?js,console

而這裏的重寫類:

class Student { 
    constructor(name, hobbies = []){ 

    this.name = name; 

    // new Set() is used to work with objects. It does not work with well with strings 
    // Let's use an array to store the hobbies. 
    // if a hobby or an hobbies array is passed, store it, otherwise set an empty array. 
    this.hobbies = this.parseHobbies(hobbies); 
    } 

    // This function will normalize the hobbies to an Array 
    parseHobbies(hobbies) { 
    if (typeof hobbies === "string") { 
     // hobbies is a string, means it's a single hobby and not an array 
     return [hobbies]; 
    } 
    // Assuming the hobbies is a an Array 
    return hobbies; 
    } 

    setName(newName) { 
    this.name = newName; 
    } 

    // this function will allow you to add a single hobby to the array 
    addHobbies(hobbies = []) { 
    // Same logic like in constract, this can accept a string or an array 
    // We use Array.concat and push to append to array 
    this.hobbies = this.hobbies.concat(this.parseHobbies(hobbies)); 
    } 

    getName() { 
    return this.name; 
    } 

    getHobbies() { 
    return this.hobbies 
    } 

    // This will return all student attributes. 
    getAttributes() { 
    // Return a copy of all the attributes instead of returning references 
    return Object.assign({}, this); 
    } 
} 

let george = new Student("George", "Sports"); 
george.addHobbies(["Singing", "Fishing"]); 
george.addHobbies("Dancing"); 
console.log(george.getAttributes());