Javascript Classes

JS Code Snippets Classes
Share:

About

In this code snippet, we’ll learn about classes in Javascript.

Classes were introduced ES6. They work the same way they do in other languages and act as templates to make objects from. Again I will just demonstrate how to use classes in javascript and won’t go into specifics of OOP(I already did some posts about it in C#)

Let’s see the code example below.

Code:

//Class definition.////////////////////////////////////////////

class Computer{
	//This is how you define a constructor...
  constructor(cpu, gpu, ram) {
   	//...and properties.
    this.cpu = cpu;
    this.gpu = gpu;
    this.ram = ram;
  }
  
  //Methods don't need the function keyword.
	boot(){
  		console.log("Checking hardware...");
      
      console.log("CPU: " + this.cpu);
      console.log("GPU: " + this.gpu);
      console.log("RAM: " + this.ram);
      
      console.log("Booting OS...");
  }
  
  shutdown(){
  	console.log("Shutting down.");
  }
  
  //And static methods don't require an instance of a class to be used.
  static getRequiredParts(){
  	console.log("You need to provide a CPU, GPU and RAM to use this computer.");
  }
}

///////////////////////////////////////////////////////////////


//Inheritance./////////////////////////////////////////////////

class Laptop extends Computer{
	constructor(cpu, gpu, ram, batteryCapacity) {
  	//Calling the parent class constructor.
    //super() can also be used for regular methods(to call the parent implementation of the method).
    super(cpu, gpu, ram);
    
    this._batteryCapacity = batteryCapacity;
  }
  
  //To override a method just redeclare it again in the derived(child) class. 
  shutdown(){
  	console.log("Shutting down laptop.");
  }
  
  //You can define getter/setter like so:
  get batteryCapacity() {
    return this._batteryCapacity;
  }
  
  set batteryCapacity(capacity) {
    this._batteryCapacity = capacity;
  }
}

///////////////////////////////////////////////////////////////


//Using the class./////////////////////////////////////////////

//Calling the static method without an instance.
Computer.getRequiredParts();

//Making an instance(object) of our class.
let myPC = new Computer("Ryzen 9 3900X", "RTX 2080 SUPER", "HyperX 64GB 3446 MHz");
myPC.boot();
myPC.shutdown();

console.log("-------------------------------------------");

//Making an instance(object) of our derived class.
let myLaptop = new Laptop("I5", "integrated", "8GB 1333 MHz", "40Wh");
myLaptop.boot();
myLaptop.shutdown();

//Get property.
console.log(myLaptop.batteryCapacity);

///////////////////////////////////////////////////////////////

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger