Javascrpt Building a Function
Here I use a simple console.log command which is similar to a print command in python
console.log("hello")
The code below shows a string that logs a # which is 2022, and the objects in a list.
function logItType(output) {
console.log(typeof output, ";", output);
}
console.log("testing out javascript")
logItType("String test"); // String
logItType(2022); // Number
logItType([5, 6, 7]); // Object is generic for this Array, which similar to Python List
Here I amde a function for the people that have names and the class they graduate. Later converting them in JSON.
// define a function to hold data for a Person
function Person(name,classOf) {
this.name = name;
this.classOf = classOf;
this.role = "";
}
// define a setter for role in Person data
Person.prototype.setRole = function(role) {
this.role = role;
}
// define a JSON conversion "method" associated with Person
Person.prototype.toJSON = function() {
const obj = {name: this.name, classOf: this.classOf, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// make a new Person and assign to variable coach
var coach = new Person("Kenji", 1970); // object type is easy to work with in JavaScript
logItType(coach); // before role
logItType(coach.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with Coach
coach.setRole("Coach"); // set the role
logItType(coach);
logItType(coach.toJSON());
Here I defined the players/coach and made a team function.
// define a student Array of Person(s)
var players = [
new Person("Jagger", 2023),
new Person("Matthew", 2023),
new Person("Gavin", 2024),
];
// define a team and build Team objects and json
function Team(coach, players){ // 1 coach, many student
// start Team with Coach
coach.setRole("Coach");
this.coach = coach;
this.team = [coach];
// add each Student to Team
this.players = players;
this.players.forEach(student => { student.setRole("Student"); this.team.push(student); });
// build json/string format of Team
this.json = [];
this.team.forEach(person => this.json.push(person.toJSON()));
}
// make a CompSci team from formerly defined coach and players
compsci = new Team(coach, players);
// output of Objects and JSON in CompSci team
logItType(compsci.team); // constructed team object
logItType(compsci.team[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
Here I converted the json into HTMl and made a table out of that
// define an HTML conversion "method" associated with Team
Team.prototype._toHtml = function() {
// HTML Style is build using inline structure
var style = (
"display:inline-block;" +
"border: 2px solid grey;" +
"box-shadow: 0.8em 0.4em 0.4em grey;"
);
// HTML Body of Table is build as a series of concatenations (+=)
var body = "";
// Heading for Array Columns
body += "<tr>";
body += "<th><mark>" + "Name" + "</mark></th>";
body += "<th><mark>" + "Class Of" + "</mark></th>";
body += "<th><mark>" + "Role" + "</mark></th>";
body += "</tr>";
// Data of Array, iterate through each row of compsci.team
for (var row of compsci.team) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + row.name + "</td>";
body += "<td>" + row.classOf + "</td>";
body += "<td>" + row.role + "</td>";
// tr to end line
body += "<tr>";
}
// Build and HTML fragment of div, table, table body
return (
"<div style='" + style + "'>" +
"<table>" +
body +
"</table>" +
"</div>"
);
};
// IJavaScript HTML processor receive parameter of defined HTML fragment
$$.html(compsci._toHtml());