Skip to content

Instantly share code, notes, and snippets.

@kiichi
Last active August 10, 2019 14:42
Show Gist options
  • Save kiichi/5db1c99aefb6c8ac921437b8aad5f856 to your computer and use it in GitHub Desktop.
Save kiichi/5db1c99aefb6c8ac921437b8aad5f856 to your computer and use it in GitHub Desktop.
Use Pokemon to teach OOP for millennials rather than dull examples like "Car" or "Animal". They don't even drive car with combustion engine.
// --------------------------
// Base
class PokeDex {
constructor(name, hp, atk, gen, type){
this.name = name;
this.hp = hp;
this.atk = atk;
this.gen = gen;
this.type = type;
this.rare = false;
}
printInfo(){
console.log('hello ' + this.name, this.hp, this.atk, this.gen + ' generation', this.type, ' rare?' + this.rare);
}
}
// -----------------------------
// Child 1
class EvolvedPokemon extends PokeDex {
constructor(name, hp, atk, gen, type){
super(name, hp, atk, gen, type);
this.evolvedDate = new Date();
}
happyBirthday(){
console.log('My birthday of evolution is ', this.evolvedDate);
}
}
// -----------------------------
// Child 2
class LegendaryPokemon extends PokeDex {
constructor(name, hp, atk, gen, type, capturedLocation){
super(name, hp, atk, gen, type);
this.rare = true;
this.capturedDate = new Date();
this.capturedLocation = capturedLocation;
}
}
////----------------------------
// Misc Class - hold various information about the location
class Location {
constructor(lat, lng, city, state, country){
this.lat = lat;
this.lng = lng;
this.city = city;
this.state = state;
this.country = country;
}
printInfo(){
console.log('('+this.lat+','+this.lng+') - ' + this.city + ',' + this.state + ' ' + this.country);
}
}
// Test -----------------------------------------
var pikachu = new PokeDex('Pikachu', 80, 20, 1, 'Electric');
pikachu.printInfo();
var squirtle = new PokeDex('Squirtle', 100, 15, 1, 'Water');
squirtle.printInfo();
var raichu = new EvolvedPokemon('Raichu', 120, 98, 1, 'Electric');
raichu.printInfo();
var newYorkGreenvale = new Location(40,-73,'Greenvale','NY','USA');
newYorkGreenvale.printInfo();
var articuno = new LegendaryPokemon('Articuno', 110, 90, 2, 'Ice', newYorkGreenvale);
articuno.printInfo();
var entei = new LegendaryPokemon('Entei', 120, 100, 2, 'Fire', newYorkGreenvale);
entei.printInfo();

In your console, you will see something like this

hello Pikachu 80 20 1 generation Electric  rare?false
hello Squirtle 100 15 1 generation Water  rare?false
hello Raichu 120 98 1 generation Electric  rare?false
(40,-73) - Greenvale,NY USA
hello Articuno 110 90 2 generation Ice  rare?true
hello Entei 120 100 2 generation Fire  rare?true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment