// define a student Array of Person(s)
var students = [
new Person("Shraddha", "ftc13184-SK", 2023),
new Person("Pravavi", "PranaviInukurti", 2023),
new Person("Madhumita", "mnarayan", 2023),
new Person("Meena", "MAnn, 2023),
new Person("Rebecca", "Rebecca-123", 2023)
];
// define a classroom and build Classroom objects and json
function Classroom(teacher, students){ // 1 teacher, many student
// start Classroom with Teacher
teacher.setRole("Teacher");
this.teacher = teacher;
this.classroom = [teacher];
// add each Student to Classroom
this.students = students;
this.students.forEach(student => { student.setRole("Student"); this.classroom.push(student); });
// build json/string format of Classroom
this.json = [];
this.classroom.forEach(person => this.json.push(person.toJSON()));
}
// make a CompSci classroom from formerly defined teacher and students
compsci = new Classroom(teacher, students);
// output of Objects and JSON in CompSci classroom
logItType(compsci.classroom); // constructed classroom object
logItType(compsci.classroom[0].name); // abstract 1st objects name
logItType(compsci.json[0]); // show json conversion of 1st object to string
logItType(JSON.parse(compsci.json[0])); // show JSON.parse inverse of JSON.stringify
object ; [ Person { name: 'Mr M', ghID: 'jm1021', classOf: 1977, role: 'Teacher' },
Person {
name: 'Anthony',
ghID: 'tonyhieu',
classOf: 2022,
role: 'Student' },
Person { name: 'Bria', ghID: 'B-G101', classOf: 2023, role: 'Student' },
Person { name: 'Allie', ghID: 'xiaoa0', classOf: 2023, role: 'Student' },
Person {
name: 'Tigran',
ghID: 'Tigran7',
classOf: 2023,
role: 'Student' },
Person {
name: 'Rebecca',
ghID: 'Rebecca-123',
classOf: 2023,
role: 'Student' } ]
string ; Mr M
string ; {"name":"Mr M","ghID":"jm1021","classOf":1977,"role":"Teacher"}
object ; { name: 'Mr M', ghID: 'jm1021', classOf: 1977, role: 'Teacher' }