遍历 javascript 中 class 的所有对象
Loop through all the objects of a class in javascript
我有一个 class Student
和一个方法 eligibleForPlacements
检查是否学生有资格获得展示位置。如何遍历所有对象并使用该方法检查学生是否符合条件?
我创建了一个数组 static allObj = new Array();
来存储所有对象,但它不起作用:
class Student {
static noOfStudents = 0;
static allObj = new Array();
constructor(name1, age, phoneNumber, boardMarks) {
this.name = name1,
this.age = age,
this.phoneNumber = phoneNumber,
this.boardMarks = boardMarks
Student.noOfStudents += 1;
Student.allObj.push(this);
}
eligibleForPlacements(minMark) {
return (age1) => {
if (age1 < this.age && this.boardMarks > minMark) {
console.log(`${this.name} is eligible for placements`);
} else {
console.log(`${this.name} is not eligible for placements`);
}
}
}
}
//created two object and iterating through it
const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);
for (let i = 0; i < Student.allObj.length; i++) {
Student[i].eligibleForPlacements(10)(5);
}
你错过了最后.allObj
class Student {
static noOfStudents = 0;
static allObj = new Array();
constructor(name1, age, phoneNumber, boardMarks) {
this.name = name1,
this.age = age,
this.phoneNumber = phoneNumber,
this.boardMarks = boardMarks
Student.noOfStudents += 1;
Student.allObj.push(this);
}
eligibleForPlacements(minMark) {
return (age1) => {
if (age1 < this.age && this.boardMarks > minMark) {
console.log(`${this.name} is eligible for placements`);
} else {
console.log(`${this.name} is not eligible for placements`);
}
}
}
}
const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);
for (let i = 0; i < Student.allObj.length; i++) {
// add .allObj here
Student.allObj[i].eligibleForPlacements(10)(5);
}
您正在尝试读取 Student
class 本身的索引,而不是 allObj
属性;将 Student[i].eligibleForPlacements(10)(5);
更改为 Student.allObj[i].eligibleForPlacements(10)(5);
并且工作正常:
class Student {
static noOfStudents = 0;
static allObj = new Array();
constructor(name1, age, phoneNumber, boardMarks) {
this.name = name1,
this.age = age,
this.phoneNumber = phoneNumber,
this.boardMarks = boardMarks
Student.noOfStudents += 1;
Student.allObj.push(this);
}
eligibleForPlacements(minMark) {
return (age1) => {
if (age1 < this.age && this.boardMarks > minMark) {
console.log(`${this.name} is eligible for placements`);
} else {
console.log(`${this.name} is not eligible for placements`);
}
}
}
}
const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);
for (let i = 0; i < Student.allObj.length; i++) {
Student.allObj[i].eligibleForPlacements(10)(5);
}
我有一个 class Student
和一个方法 eligibleForPlacements
检查是否学生有资格获得展示位置。如何遍历所有对象并使用该方法检查学生是否符合条件?
我创建了一个数组 static allObj = new Array();
来存储所有对象,但它不起作用:
class Student {
static noOfStudents = 0;
static allObj = new Array();
constructor(name1, age, phoneNumber, boardMarks) {
this.name = name1,
this.age = age,
this.phoneNumber = phoneNumber,
this.boardMarks = boardMarks
Student.noOfStudents += 1;
Student.allObj.push(this);
}
eligibleForPlacements(minMark) {
return (age1) => {
if (age1 < this.age && this.boardMarks > minMark) {
console.log(`${this.name} is eligible for placements`);
} else {
console.log(`${this.name} is not eligible for placements`);
}
}
}
}
//created two object and iterating through it
const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);
for (let i = 0; i < Student.allObj.length; i++) {
Student[i].eligibleForPlacements(10)(5);
}
你错过了最后.allObj
class Student {
static noOfStudents = 0;
static allObj = new Array();
constructor(name1, age, phoneNumber, boardMarks) {
this.name = name1,
this.age = age,
this.phoneNumber = phoneNumber,
this.boardMarks = boardMarks
Student.noOfStudents += 1;
Student.allObj.push(this);
}
eligibleForPlacements(minMark) {
return (age1) => {
if (age1 < this.age && this.boardMarks > minMark) {
console.log(`${this.name} is eligible for placements`);
} else {
console.log(`${this.name} is not eligible for placements`);
}
}
}
}
const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);
for (let i = 0; i < Student.allObj.length; i++) {
// add .allObj here
Student.allObj[i].eligibleForPlacements(10)(5);
}
您正在尝试读取 Student
class 本身的索引,而不是 allObj
属性;将 Student[i].eligibleForPlacements(10)(5);
更改为 Student.allObj[i].eligibleForPlacements(10)(5);
并且工作正常:
class Student {
static noOfStudents = 0;
static allObj = new Array();
constructor(name1, age, phoneNumber, boardMarks) {
this.name = name1,
this.age = age,
this.phoneNumber = phoneNumber,
this.boardMarks = boardMarks
Student.noOfStudents += 1;
Student.allObj.push(this);
}
eligibleForPlacements(minMark) {
return (age1) => {
if (age1 < this.age && this.boardMarks > minMark) {
console.log(`${this.name} is eligible for placements`);
} else {
console.log(`${this.name} is not eligible for placements`);
}
}
}
}
const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);
for (let i = 0; i < Student.allObj.length; i++) {
Student.allObj[i].eligibleForPlacements(10)(5);
}