Hey.. So far we’ve only been talking about various Algorithms like Sorting and Searching all these while. Today we will take a different direction to talk about Data Structures which are how Data is Presented, Stored or Partitioned in a storage memory before any algorithm can run on them. the assembly of the above is what I refer to as Data Structures. The Most common Data Structure we all are familiar with is Arrays and we know in arrays we have linear type of storage where every element in a list has an index that keeps track on their values. other Data Structures such as Trees, Linked Lists, Maps, Heaps etc… are what we may probably be having a look at in the next few sections. Most of these structures in JavaScript are written in a class base style as a blueprint for any Data Structures. other ways like pure functions style could also be used but it makes primitive sense to achieve this with a class constructor, JavaScript itself is not an object oriented programming language but rather a prototype based scripting language. We write these structures most of the time to behave like objects.
JavaScript ES2016 makes it more easier for us to have those classes written with less efforts, let’s have a look at a simple class definition in code.
class Student {
constructor(firstName, lastName, level) {
this.firstName = firstName;
this.lastName = lastName;
this.year = level;
this.tardies = 0;
this.scores = [];
}
}
in simple term that is how a class object is created by using the class keyword followed by the name of the class (Student) a class method in this case should always have a constructor that define the instances of the object under construction and here the instances involved are firstName, lastName, level which are just in normal function arguments or parameters on which the function will run. The this keyword in this context binds each definition to the instances supplied to the constructor, which means when requesting for any key on the class those assigned with the this variables are going to be responsible for calling their provided values ie. to get a supplied firstName the this.firstName is going to be the instance to be called, equally to get the level value the this.year is going to be called but not the assigned argument which is level they are just to match the real instances of the class constructors.
Now that we have a class we can’t start jubilating but then let’s ask some provoking questions as to where from those Algorithms we wrote earlier and what are their places in this class construction hell. well good question but the answer is simple they are methods on which the structures run for instance push, pop, sort, slice, splice etc…
Since those methods sometimes can not fully do what we expect to get from them, there is a need for us to build our own in order to be sure to get all the answers to our doubts. So let’s build some Methods(Functions) on our class to get the best out of it.
class Student {
constructor(firstName, lastName, level) {
this.firstName = firstName;
this.lastName = lastName;
this.year = level;
this.tardies = 0;
this.scores = [];
}
fullName() {
return `Your Fullname is
${this.firstName}
${this.lastName}`;
}
attendance() {
this.tardies += 1;
if (this.tardies >= 3) {
return 'You are Expelled !!!';
}
return `${this.firstName} ${this.lastName}
has been late ${this.tardies} times`;
}
addScore(score) {
this.scores.push(score);
return this.scores;
}
calculateAverage() {
let sum = this.scores.reduce(function (a, b) {
return a + b;
});
return `AVG: ${Math.floor(sum / this.scores.length)}`;
}
static EnrollStudents() {
return 'ENROLLING STUDENTS!';
}
}
We built about 5 Methods on our Student Class Object which are:
fullName: a method to get the fullname of any new student
attendance: to read the attendance of a given student
addScore: for adding a new score to the student academics
calculateAverage: calculate the average marks of a student
Finally a very special one EnrollStudent which is called a Static method because it’s purpose is to have a method alright but the one which will not be related to any of the instances provided in the class but will serve as a purpose in the life cycle of the object, as you can see in the above case it’s just returning a message which reads ‘ENROLLING STUDENTS’. To access these methods created above we just define a new instance of our class (Student) and provide the required arguments to have access to the various methods that come with the class.
let firstStudent = new Student('Kofi', 'Mensah', 3);
let secondStudent = new Student('Akua', 'Fosu', 5);
To now use the various methods we created in the class we could access them like this.
//firstStudent.fullName();
firstStudent.markLate();
//firstStudent.addScore(94);
//Student.EnrollStudents();
Run all These methods in whichever console you’re testing and you will see all this playout to supplement what we have discussed so far.
I hope my bad English made a little sense in this post. until next time keep practicing, have a nice time bye 🙂