Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active January 15, 2021 15:39
Show Gist options
  • Save ross-u/5a919d6285dd3d2b67f0f05907c70953 to your computer and use it in GitHub Desktop.
Save ross-u/5a919d6285dd3d2b67f0f05907c70953 to your computer and use it in GitHub Desktop.

JS | Hero Factory - OOP Exercise (ES6)



For this exercise you can use Repl.it or your code editor.


Using the below file hero-factory.js finish the tasks as specified in the comments in the script.

We included the Syntax Reminder snippets below, to help you with the task:


Syntax Reminder:

ES6 class constructor - syntax

class Car {
  constructor(brand, model) {
    // in the constructor we specifiy parameters that will be passed
    this.brand = brand; // create property on the new instance
    this.model = model;
  }

  start() {
    // create method on the Prototype
    console.log(`${this.brand} Engine start`);
  }
}


const bmw = new Car("BMW", "750i");
// Car { brand: "BMW", model: "750i" }

bmw.start();

Extending the constructor - syntax

class HybridCar extends Car {
  constructor(brand, model, engineType) {
    // call class `Car()` via `super()` - which creates properties `brand` and `model` on the new instance
    super(brand, model);
    this.engineType = engineType; // create an additional property on the new instance
  }

  // creates a method on the HybridCar prototype
  description () {
    console.log(`Brand: ${this.brand}. Engine: ${this.engineType}`);
  }

  // creates a static method that can be called only from class HybridCar
  static printClassName() {
    console.log("class name is HybridCar");
  }
}


const toyotaHybrid = new HybridCar("Toyota", "Prius", "Hybrid");
// HybridCar {brand: "Toyota", model: "Prius", engine: "Hybrid"}

toyotaHybrid.description(); //  from the HybridCar.prototype -> bmwHybrid.__proto__
toyotaHybrid.start(); //	from the Car.prototype -> bmwHybrid.__proto__.__proto__

Happy Coding! 🚀


/*
1.
a) Create a ES6 class called "HeroClass" which creates and returns objects with properties
"name", "superpowers", "costumeColor", "location".
b) Create the methods "heroDescription" and "heroLocation" on the prototype of the `HeroClass`.
Method `heroDescription` should log to the console: `${name} - ${superpower} - ${location}`.
Method `heroLocation` should log to the console: `${name} is hidding in ${location}`.
c) Complete the `static` method "className" to the `HeroClass` (as provided), so that it logs `"HERO CLASS"` to the console.
Remember that Class constructor must use keyword `new` when called to create new object.
*/
class HeroClass {
constructor(name, superpowers, costumeColor, location) {
// Your code here ..
}
static className() {
// Your code here ..
}
}
const bananaman = new HeroClass(
"Banananman",
"Flight, Invulnerability, Helium-boosted heat finger",
"blue and yellow",
"Metropolis"
);
console.log(bananaman);
bananaman.heroDescription(); // Expected: "Banananman - Flight, Invulnerability, Helium-boosted heat finger - Metropolis"
bananaman.heroLocation(); // Expected: "Banananman is hidding in Metropolis"
HeroClass.className(); // Expected: "HERO CLASS"
/*
2.
Create a class "SuperHeroClass" which extends "HeroClass" and has one additional new property
"immortal" which holds a boolean value.
Remember to call/include `super()` inside of the `constructor() {}` block and pass it the arguments for the `HeroClass`.
*/
class SuperHeroClass extends HeroClass {
constructor(name, superpowers, costumeColor, location, immortal) {
//
// Your code here ..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment