Beginners guide to JavaScript Classes
Tags: javascript, webdev, beginners, codenewbie
Published: 2020-12-15 06:01:32

I have recently joined this platform and this is my first post here hoping that you'll like it. So a bit about me - I'm Emma. Front-end Developer and Blogger. I focus on teaching more so I will be posting articles related to JavaScript, CSS for beginners. 

<hr>

## 😳 Classes In JavaScript? 

Classes were introduced in JavaScript ECMA2015(2016). 
Unlike the classes in Object-Oriented Model. But instead of classes are just a special type of functions. But instead of using the "function" keyword, we use the "class". 

It was introduced in JavaScript to make the syntax look like other object-oriented languages (Java, Python, C++). 


### 😃 Defining a Class

```Javascript
class Rectangle {
 constructor(height, width) {
   this.height = height; 
   this.width = width; 
 }
}
```

To declare a **class**, you use the **class** keyword with the name of the class (**"Rectangle"** here). 

**Constructor **: This is a special method for initializing an instance of that class. So what that means is that whenever we create a new instance of the class, it will invoke the constructor.  

### 📘 Methods in a Class

```javascript
class Rectangle {
  constructor(height, width) {
    this.height = height; 
    this.width = width; 
  }
  //prototype method
  area () {
    return console.log(`The area is ${this.height*this.width}`); 
  }
  //static method
  static display(rect) {
    return console.log(`Height: ${rect.height} Width: ${rect.width}`); 
  }
}

rectangle1 = new Rectangle(5,4); //instantiate the class
rectangle1.area(); 
//the area is 20
Rectangle.display(rectangle1); 
//Height: 5 Width: 4
```

- **Prototype Method**: area() is a prototype method. 
- **Static Method**: display() is a static method. 

### 🤨 Prototype Method

A prototype method is a method that is only accessible when you create an instance of the class. 

As you can see in the example above, you call the prototype method by referring to the object's method name followed by parentheses (any parameter would go inside the parentheses). 

### 🤔 Static Method

A static method is something you can call without instantiating the class. The static method is defined in the class itself, and not on the object. That means you cannot call the static method on the object (rectangle1), but on the class (Rectangle) as shown in *line 19*. 

### ⭐ Inheritance

```javascript
class car {
  constructor(brand) {
    this.carname = brand; 
  }
  present() { 
    return `I have a ` + this.carname; 
  }
}

class Model extends Car {
  constructor(brand, mode) {
    super(brand); 
    this.model = mod; 
  }
  show() {
    return console.log(`${this.present()} , it is a ${this.model}`); 
  }
}

mycar = new Model("Ford", "Raptor"); 
mycar.show(); 
//I have a ford , it is a Raptor
```

To create a class inheritance, use the **extends** keyword. 

<hr>

A class created with class inheritance inherits all the methods from another class. In the above example, the Model class inherits all the methods from **Car** class. 

The **super()** method refers to the parent class. 

By calling the **super()** method in the constructor methods, we call the parent's constructor method and gets access to the parent's properties and methods. 

Inheritance is useful for **code reusability**, we can reuse properties and methods of an existing class when you create a new class. 

<hr>
Get the amazing  [daily.dev](https://api.daily.dev/get?r=emmaturner) extension. You'll get amazing news and ideas.
<hr>

Thanks For Reading. If you have anything on your mind comment below. You can follow me too. 🙂📚