Javascript,添加构造函数的类

问题描述:

我有点新的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以及我在此之前尝试声明爱好功能,没有像我想要的那样工作。 –

试试这个:

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

我在这里设置或里面的功能?

这取决于你需要什么。你是否想让每个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) 
} 

你是在正确的方向。我已经重写了你的班级,去做我认为更接近你想要达到的目标的班级。

播放与代码: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());