javascript JS无法阅读调用[已关闭]

epfja78i  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(128)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
13小时前关门了。
Improve this question
这是我想自己编的代码,但是调用不起作用

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);

这是我想自己编的代码,但是调用不起作用

yhxst69z

yhxst69z1#

您指的是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);

相关问题