Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Last active January 4, 2024 17:25
Show Gist options
  • Save prof3ssorSt3v3/f38f884d1b73af1b997b943769376608 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/f38f884d1b73af1b997b943769376608 to your computer and use it in GitHub Desktop.
Starter files for Class Exercise
class Burger {
#isMeatless = false;
#isCooked = false;
toppings = [];
constructor(_isMeatless = false, ...toppings) {
this.#isMeatless = _isMeatless;
this.toppings = toppings;
}
get toppings() {
return [...toppings];
}
isVeggie() {
return this.#isMeatless;
}
static cookBurger(burger) {
return new Promise((resolve, reject) => {
setTimeout(() => {
burger.#isCooked = true;
console.log('burger is cooked');
resolve();
}, 2000);
});
}
}
export default Burger;
//Example usage of the class.
let b = new Burger(false, 'cheese', 'lettuce', 'tomato', 'onion');
console.log(b.toppings);
console.log(b.isVeggie());
Burger.cookBurger(b);
class Fish {
#_color;
#_numFins;
constructor(_color, _numFins = 3) {
this.#_color = _color;
this.#_numFins = _numFins;
}
get color() {
return this.#_color;
}
get numberOfFins() {
return this.#_numFins;
}
swim(_speed) {
if (isNaN(_speed)) throw Error('Speed must be numeric');
let output = Array(_speed).fill('Swish').join(', ');
console.log(output);
return output;
}
}
export default Fish;
//Example usage of the class
let jaws = new Fish('grey', 3);
console.log(jaws.color);
console.log(jaws.numberOfFins);
jaws.swim(8);
class Person {
#firstName;
#lastName;
constructor(_first, _last) {
this.#firstName = _first;
this.#lastName = _last;
}
get name() {
return `${this.#firstName} ${this.#lastName}`;
}
talk(words, element) {
//element is the elementReference or id of an element
console.log(words);
let ref;
if (typeof element == 'string') {
ref = document.querySelector(element);
} else {
if (typeof HTMLElement != 'undefined' && element instanceof HTMLElement) {
ref = element;
}
}
if (ref) ref.textContent = words;
}
}
export default Person;
//Example usage of class
let bob = new Person('Bobby', 'Singer');
console.log(bob.name);
bob.talk(`Hello my name is ${bob.name}.`);
class Robot {
#_color;
#isShiny = false;
purpose = '';
constructor(_color, _isShiny, _purpose) {
this.#_color = _color;
this.#isShiny = _isShiny;
this.purpose = _purpose;
}
get color() {
let str = `The robot is ${this.#_color}`;
str += this.#isShiny ? ' and shiny.' : ' but not shiny.';
return str;
}
talk(element) {
//element is the elementReference or id of an element
console.log('Beep');
let ref;
if (typeof element == 'string') {
ref = document.querySelector(element);
} else if (typeof HTMLElement != 'undefined' && element instanceof HTMLElement) {
ref = element;
} else {
console.log('Beep');
}
if (ref) ref.textContent = 'Beep';
}
}
export default Robot;
//Example usage of class
let marvin = new Robot('white', true, 'being depressed');
console.log(marvin.color);
console.log(marvin.purpose);
marvin.talk();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment