Hey! It's day 12 of my 100days of code challenge and I will take you through classes in ES6. Objects are created using a blueprint or let's say a template. Classes are the blueprints for creating these objects. They are composed of constructors and methods.
Constructors are used for allocating memory space to objects and methods are functions that tell how objects behave or act.
A person for example is an object which has features like name, height, weight and performs functions like working, eating, exercise e.t.c Bit every person has unique features and unique ways of performing these functions. A person might be small or big, eats small or big.
We can create a class of person and just give each unique person, a unique feature and function.
class Person {
constructor(name,height,position) {
this._name = name;
this._height = height;
this._position = position;
}
doWork() {
return "I am a" + this._position ;
}
}
p1= new Person("Ade","20","Engineer");
p2 = new Person("Abdullah","25","Accountant");
With p1 and P2 above, I created two person with unique names,heights and positions. What I did is referred to as object instantiation.
Inside the class Person, I also created a method doWork which can be invoked or called on p1 or p2.
I hope I have been able to do justice explaining classes in ES6.