-Which Data Structure is The Best?
-ES2015 Class Syntax Overview
-Data Structure: The Class Keyword
-Data Structure: Adding Instance Methods
Which Data Structure is The Best?
Data structures are collections of values, the relationships among them, and the functions or operations that can be applied to the data. Data structures excel at different things. Some are highly specialized, while others are more generally used.
Visualized Data Structures
Array
Singly Linked List
Hash Table
Tree
Binary Search Tree
Undirected Graph
Directed Graph
All data structures store data, however the relationship between the data and the functionality between methods differ.
ES2015 Class Syntax Overview
What is a class?
A class is a blueprint for creating objects with pre-defined properties and methods.
Class example
class Student {
constructor(firstName, lastName, year){
this.firstName = firstName;
this.lastName = lastName;
this.grade = year;
}
}
let firstStudent = new Student("Colt", "Steele",1);
let secondStudent = new Student("Blue", "Steele",2);
The method to create new objects must be called constructor.
The class keyword creates a constant that can not be redefined.
Data Structure: Adding Instance Methods
class Student {
constructor(firstName, lastName, year){
this.firstName = firstName;
this.lastName = lastName;
this.grade = year;
this.tardies = 0;
this.scores = [];
}
fullName(){
return `Your full name is ${this.firstName} ${this.lastName}`;
}
markLate(){
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 sum/this.scores.length;
}
}
let firstStudent = new Student("Colt", "Steele",1);
let secondStudent = new Student("Blue", "Steele",2);
Top comments (3)
Nice high-level overview, I can never have enough quick references and cheatsheets.
Good overview to DataStructures.
Great introduction on the DS types. I also needed that refresher on class syntax 😁.