Skip to content

Instantly share code, notes, and snippets.

@MartaJank
Forked from ross-u/README.md
Created October 23, 2020 14:43
Show Gist options
  • Save MartaJank/1932ec4eb7b9e3436a84322f94db90a8 to your computer and use it in GitHub Desktop.
Save MartaJank/1932ec4eb7b9e3436a84322f94db90a8 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");
  }
}

var 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