JavaScript UI Development
console.log("Hi");
function logItType(output) {
console.log(typeof output, ";", output); //"typeof" keyword returns the type.
}
function Person(name, email, classOf) {
this.name = name;
this.email = email;
this.classOf = classOf;
this.role = "";
}
Person.prototype.setRole = function(role) {
this.role = role;
}
Person.prototype.toJSON = function() {
const obj = {name: this.name, ghID: this.email, classOf: this.classOf, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// make a new Person and assign to variable teacher
var teacher = new Person("Mr M", "jmortensen@powayusd.com", 1977); // object type is easy to work with in JavaScript
logItType(teacher); // before role
logItType(teacher.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with Teacher
teacher.setRole("Teacher"); // set the role
logItType(teacher);
logItType(teacher.toJSON());
var students = [
new Person("Krish Patil", "krishpatil1019@gmail.com", 2024),
new Person("Don Tran", "donqt15@gmail.com", 2024),
new Person("Nathan Manango", "prorichyman@gmail.com", 2024),
new Person("Nicholas Ramos", "nicky.jay77@gmail.com", 2024),
];
function Classroom(teacher, students){
teacher.setRole("Teacher");
this.teacher = teacher;
this.classroom = [teacher];
this.students = students;
this.students.forEach(student => { student.setRole("Student"); this.classroom.push(student); });
this.json = [];
this.classroom.forEach(person => this.json.push(person.toJSON()));
}
compsci = new Classroom(teacher, students);
logItType(compsci.classroom);
logItType(compsci.classroom[0].name);
logItType(compsci.json[0]);
logItType(JSON.parse(compsci.json[0]));
Classroom.prototype._toHtml = function() {
var style = (
"display:inline-block;" +
"border: 2px solid white;" +
"box-shadow: 0.8em 0.4em 0.4em black;"
);
var body = "";
body += "<tr>";
body += "<th><mark>" + "Name" + "</mark></th>";
body += "<th><mark>" + "Email" + "</mark></th>";
body += "<th><mark>" + "Class Of" + "</mark></th>";
body += "<th><mark>" + "Role" + "</mark></th>";
body += "</tr>";
for (var row of compsci.classroom) {
body += "<tr>";
body += "<td>" + row.name + "</td>";
body += "<td>" + row.email + "</td>";
body += "<td>" + row.classOf + "</td>";
body += "<td>" + row.role + "</td>";
body += "<tr>";
}
return (
"<div style='" + style + "'>" +
"<table>" +
body +
"</table>" +
"</div>"
);
};
$$.html(compsci._toHtml());