【发布时间】:2023-02-25 01:19:26
【问题描述】:
我需要 getData 方法将 this.name 和 this.assignatures 作为对象返回。
class Student {
constructor(name, assignatures) {
this.name = name;
this.assignatures = assignatures = [ "Javascript", "HTML", "CSS" ];
}
getData() {
return [ this.name, this.assignatures ]
}
}
let newStudent = new Student("Tucu")
console.log(newStudent)
let dataNewStudent = newStudent.getData();
console.log(dataNewStudent)
【问题讨论】:
-
您是否尝试过使用object initializer?
-
使用 {} 而不是 []。 [] 是一个数组
-
为什么不只是
return this?它已经是一个对象。
标签: javascript object methods