2016-12-07 10 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);'は 'hobby'がそのスコープに存在しないので動作しません。 –

+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」に対して1つのタイプを持つ方が良いことは1つだけです。つまり、コンストラクタでは、1つの要素の配列を作成する必要があります。つまり、Setをユニークに設定する必要があります。 – ZedXter

+0

@Bergiそうです、TypeScriptを自分で使い、そこから取り出しました。私は私の答えを改訂した。ありがとう! – Shai

1

設定しません?

これは必要なものによって異なります。それぞれのStudentに趣味のセットを1つ置きたいのですか、またはその関数が呼び出されるたびに新しいセットを作成しますか?まったく動作しない

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

。関数の値を使ってプロパティを作成していますが、メソッドが呼び出されると、戻り値がadd methodで上書きされます。


それを動作させるために、私は.hobbiesinstance 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()); 
関連する問題