Popcorn Hack #1
Create a child class of the class Appliance, and call it’s name() function
%%js
class Mercedes {
constructor(name) {
this.name = name;
}
returnname() {
return "I am a " + this.constructor.name + " and my model is " + this.name;
}
}
// Below this name the class and cause it to inherit from the Appliance class
class MercedesC63 extends Mercedes{
constructor(name) {
super(name);
}
}
var Benz = new Mercedes('C63');
console.log(Benz.returnname());
<IPython.core.display.Javascript object>
Popcorn Hack #2
Create child classes of the product class with items, and add parameters depending on what it is. An example is provided of a bagel.
%%js
class Product{
constructor(price,size,taxRate) {
this.price = price;
this.size = size;
this.taxRate = taxRate;
}
getPrice() {
return this.price + this.taxRate * this.price;
}
product(){
const className = this.constructor.name.toLowerCase();
return "You are ordering a " + className + " with a price of " + this.getPrice() + " dollars, a size of " + this.size;
}
}
class Bagel extends Product{
constructor(price, size, taxRate, flavor) {
super(price, size, taxRate);
this.flavor = flavor;
}
getPrice(){
return super.getPrice() * this.size;
}
product(){
return super.product() + " and a flavor of " + this.flavor;
}
}
var sesameBagel = new Bagel(1.5, 2, 0.07, "sesame");
console.log(sesameBagel.product());
<IPython.core.display.Javascript object>
class Product {
constructor(price, size, taxRate) {
this.price = price;
this.size = size;
this.taxRate = taxRate;
}
getPrice() {
return this.price + this.taxRate * this.price;
}
product() {
const className = this.constructor.name.toLowerCase();
return "You are ordering a " + className + " with a price of " + this.getPrice() + " dollars, a size of " + this.size;
}
}
class Car extends Product {
constructor(price, size, taxRate, brand, model, horsepower) {
super(price, size, taxRate);
this.brand = brand;
this.model = model;
this.horsepower = horsepower;
}
product() {
return super.product() + ", brand: " + this.brand + ", model: " + this.model + " with " + this.horsepower + " horsepower";
}
}
class MercedesC63AMG extends Car {
constructor(price, size, taxRate, horsepower) {
super(price, size, taxRate, "Mercedes", "C63 AMG", horsepower);
}
// You can override methods or add more specific ones here
}
var mercedesC63 = new MercedesC63AMG(70000, "medium", 0.10, 503);
console.log(mercedesC63.product());