Это код, который я хочу сделать сам, но вызов не работает

            let course = {
                name: "",
                description: "",
                students: [],
                addStudents(studentName) {
                    this.students.push(studentName);
                    console.log(`${studentName} added to ${this.name}course`);
                },
                date: "12/12/2021",
            };
            let english = {
                name: "english course",
                description: "this is good course",
                students: [],
            };
            let math = {
                name: "math course",
                description: "this is very good course",
                students: [],
            };
            let addStudents = math.addStudents;
            addStudents.call(english, "Enoh");
            addStudents.call(math, "Daniel");
            addStudents.call(english, "Ellias");
            addStudents.call(math, "Rafael");
            console.log(math);
            console.log(english);

Это код, который я хочу сделать сам, но вызов не работает

-2
Sanu 23 Янв 2023 в 21:58
1
let addStudents = course.addStudents;
 – 
Unmitigated
23 Янв 2023 в 22:00

1 ответ

Вы имели в виду course.addStudents, а не math.addStudents, прочитайте ошибки в консоли

let course = {
  name: "",
  description: "",
  students: [],
  addStudents(studentName) {
    this.students.push(studentName);
    console.log(`${studentName} added to ${this.name}course`);
  },
  date: "12/12/2021",
};
let english = {
  name: "english course",
  description: "this is good course",
  students: [],
};
let math = {
  name: "math course",
  description: "this is very good course",
  students: [],
};
let addStudents = course.addStudents;
addStudents.call(english, "Enoh");
addStudents.call(math, "Daniel");
addStudents.call(english, "Ellias");
addStudents.call(math, "Rafael");
console.log(math);
console.log(english);
0
Konrad 23 Янв 2023 в 21:59