// define a function to hold data for a Person
function Person(name, nationality, age) {
this.name = name;
this.nationality = nationality;
this.age = age;
this.role = "";
}
// define a setter for role in Person data
Person.prototype.setRole = function(role) {
this.role = role;
}
// defines the manager
var manager = new Person("Qualcomm", "QCOM", "$120.40");
manager.setRole("Manager");
// JSON conversion from object to a string
Person.prototype.toJSON = function() {
const obj = {name: this.name, nationality: this.nationality, age: this.age, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// player data
var players = [
new Person("TSLA", "Telsa", "$277.20"),
new Person("AAPL", "Apple", "$150.6" ),
new Person("AMZN", "Amazon", "$115.20"),
new Person("LCID", "Lucid", "$14.50"),
new Person("RIVN", "Rivian", "$33.85"),
];
var FUT = new Squad(manager, players);
Squad.prototype._toHtml = function() {
var style = (
"display:inline-block;" +
"background:black;" +
"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>" + "Ticker" + "</mark></th>";
body += "<th><mark>" + "Company" + "</mark></th>";
body += "<th><mark>" + "Price" + "</mark></th>";
body += "</tr>";
// Data of Array, iterate through each row of lakers team
for (var row of FUT.squad) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + row.name + "</td>";
body += "<td>" + row.nationality + "</td>";
body += "<td>" + row.age + "</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(FUT._toHtml());