Jupyter JavaScript Kernel
Jupyter Notebook that shows personal usage of Jupyter JavaScript Kernel
- console.log output
- console.log output showing use of variable
- console.log output showing use of function
- Showing reuse of a function
- Dynamic or Loosely typed language (string, number)
- Build a Person Function/Group object and JSON
- Create new people in group
- IJavaScript and Table formatting using toHTML method
console.log("Hey Guys!");
var msg = "Hey Guys!";
console.log(msg)
function logIt(output) {
console.log(output);
}
logIt(msg);
Showing reuse of a function
Now that a function is defined, it can be called from any of the subsequent cell in the Jupyter notebook. A function/method, is a process of creating a procedural abstraction. This a programming practice to promote reuse versus coding the same thing over and over.
- First call sends a different string message
- Second call sends a number
console.log("Hey Y'all")
logIt("How are you doing?");
logIt(2022)
Dynamic or Loosely typed language (string, number)
JavaScript is a loosely typed language, don't have to specify what type of information will be stored in a variable in advance. This is similar to Python and most interpretive languages. Java which is a compiled language is strongly typed, thus you will see string, integer, double, and object in the source code. In JavaScript, the "typeof" keyword returns the type.
function logItType(output) {
console.log(typeof output, ";", output);
}
console.log("Different types of outputs")
logItType("hey guys"); // String
logItType(2022); // Number
logItType([1, 2, 3]); // Object is generic for this Array, which similar to Python List
Build a Person Function/Group object and JSON
JavaScript functions have special properties and syntax is shown in many ways. In fact, a Class in JavaScript is a special function. Jupyter Notebooks seems to be more friendly to "function" definitions versus "Class", thus this lesson uses "function" and "prototype" versus "Class".
- Definition of function allows for a collection of data
- Definition of a prototype allow for the definition of a method associated with the function
- Instance of a function
// define a function to hold data for a Person
function Person(name, favcolor, age) {
this.name = name;
this.favcolor = favcolor;
this.age = age;
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, favcolor: this.favcolor, age: this.age, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// make a new Person and assign to variable upperclassmen
var upperclassmen = new Person("Abby", "pink", 17); // object type is easy to work with in JavaScript
logItType(upperclassmen); // before role
logItType(upperclassmen.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with Teacher
upperclassmen.setRole("upperclassmen"); // set the role
logItType(upperclassmen);
logItType(upperclassmen.toJSON());
// define a student Array of Person(s)
var underclassmen = [
new Person("Olivia", "purple", 14),
new Person("Allie", "green", 15),
new Person("Claire", "pink", 15)
];
// define a group and build Group objects and json
function Group(upperclassmen, underclassmen){
// start group with upperclassmen
upperclassmen.setRole("upperclassmen");
this.upperclassmen = upperclassmen;
this.group = [upperclassmen];
// add each person to group
this.underclassmen = underclassmen;
this.underclassmen.forEach(underclassmen => { underclassmen.setRole("underclassmen"); this.group.push(underclassmen); });
// build json/string format of group
this.json = [];
this.group.forEach(person => this.json.push(person.toJSON()));
}
// make a del norte group from formerly defined uper and under classmen
delnorte = new Group(upperclassmen, underclassmen);
// output of Objects and JSON in del norte group
logItType(delnorte.group); // constructed classroom object
logItType(delnorte.group[0].name); // abstract 1st objects name
logItType(delnorte.json[0]); // show json conversion of 1st object to string
logItType(JSON.parse(delnorte.json[0])); // show JSON.parse inverse of JSON.stringify
// define an HTML conversion "method" associated with Group
Group.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>" + "Favorite Color" + "</mark></th>";
body += "<th><mark>" + "Age" + "</mark></th>";
body += "<th><mark>" + "Role" + "</mark></th>";
body += "</tr>";
// Data of Array, iterate through each row of delnorte.group
for (var row of delnorte.group) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + row.name + "</td>";
body += "<td>" + row.favcolor + "</td>";
body += "<td>" + row.age + "</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(delnorte._toHtml());