Как передать один аргумент в метод вместо 3 определенных

249
13 июля 2018, 18:30

Когда пытаюсь скомпилировать код, вижу такую ошибку: "Expected 3 arguments, but got 1" на мутод "refactorStudent" и "refactorInfo", подскажите как пофиксить. Да и вообще, правильно ли я использую TypeScript (только начал).

//constructor for creating students  
class Student { 
 
    constructor(public name: string, public surname: string, public age: number) { 
        this.name = name; 
        this.surname = surname; 
        this.age = age; 
    } 
    //rewrite one or all options of student info  
    public refactorStudent(name: string, surname: string, age: number): void { 
 
        this.name = typeof name !== 'undefined' ? name : this.name; 
        this.surname = typeof surname !== 'undefined' ? surname : this.surname; 
        this.age = typeof age !== 'undefined' ? age : this.age; 
    } 
    //show info about student  
    public toString(): string { 
        return `\nName: ${this.name}, Surname: ${this.surname}, Age: ${this.age}`; 
    } 
} 
//constructor for create groups with students  
class Group { 
    public students: any[]; 
    constructor(public nameGroup: string, public course: string, public specialization: string) { 
        this.nameGroup = nameGroup; 
        this.course = course; 
        this.specialization = specialization; 
        this.students = []; 
    } 
    //rewrite one or all options of group info  
    public refactorInfo(nameGroup: string, course: string, specialization: string): void { 
        this.nameGroup = typeof nameGroup !== 'undefined' ? nameGroup : this.nameGroup; 
        this.course = typeof course !== 'undefined' ? course : this.course; 
        this.specialization = typeof specialization !== 'undefined' ? 
            specialization : this.specialization; 
    } 
    //add new student 
    public addStudent(...students: any[]) { 
        this.students.push(...students); 
    } 
    // show info about group with students 
    public toString(): string { 
        return `Name of Group: ${this.nameGroup}; \nCourse: ${this.course}; \nSpecialization:  
${this.specialization};\nStudents: ${this.students}` 
    } 
} 
// create students 
let student1 = new Student("Aladin", "Indus", 20); 
let student2 = new Student("Allah", "Babah", 25); 
let student3 = new Student("Krishna", "harehare", 23); 
 
// create group 
let Devs = new Group("D-11", "4", "Front-end"); 
//rename group name 
Devs.refactorInfo("D-12"); 
//push students into group 
Devs.addStudent(student1, student2, student3); 
//rename name of first student 
student1.refactorStudent("Super"); 
console.log(Devs.toString());

Answer 1

Если хочешь чтоб аргумент был не mandatory то добавь "?" как я написал ниже.

public refactorStudent(name?: string, surname?: string, age?: number): void {
    // ....
}
READ ALSO
Как проитерировать объект и получить на выходе новый с Object.keys()

Как проитерировать объект и получить на выходе новый с Object.keys()

У меня есть на входе объект, как вернуть на выходе измененный объект на основе сущствующего?

244
Переход к другому файлу JS

Переход к другому файлу JS

UPD Есть файл indexhtml и index2

266
Как получить координаты мышки в javascript без jQuery?

Как получить координаты мышки в javascript без jQuery?

Скиньте пожалуйста самый простой пример с двумя input, в которых будут обновляться координаты мышки по горизонтали и вертикалиБез jQuery , спасибо...

246