Data Structure | Introduction

Data Structure | Introduction

Data Structures are a collection of values, the relationship between them and the functions or operations that can be applied to the data.

What are the different data structures available?

The most commonly used data structures are

  1. Singly Linked Lists
  2. Doubly Linked Lists
  3. Stacks
  4. Queues
  5. Binary Search Tree
  6. Binary Heaps
  7. Hash Tables
  8. Graphs and many more..

Why are there so many different data structures?

Different data structures excel at different things. Some are highly specialised , while others are more generally used

Class Keyword

Javascript does not come with many of these data structures pre built. Class keyword was introduced in Javascript as a part of ES2015.

Class is like a blueprint to create objects with pre defined properties and methods.

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 classalike semantics

One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name of the class in the example below the name of the class is "Rectangle".

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

A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class's body. (it can be retrieved through the class's (not an instance's) name property, though).

// unnamed
let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle"

// named
let Rectangle = class Rectangle2 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle2"

Constructor

The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.

A constructor can use the super keyword to call the constructor of the super class.

"this" keyword refers to the property of that particular object that is being called

Instance Methods

Instance methods refer to the functions that can be written inside a class, such that the function is called on each object that is being created. In example below, the function area() can be called on 'square' and 'square1' separately.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);
const square1 = new Rectangle(20, 10);

console.log(square.area); // 100
console.log(square1.area); // 200

Static Methods

Static methods refer to the functions that can be written inside a class, such that the function is called NOT on each object that is being created but can be called using the class itself. Static members (properties and methods) are called without instantiating their class and cannot be called through a class instance. Static methods are often used to create utility functions for an application, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances A method can be made into a static method by adding 'static' keyword before it.

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  static displayName = "Point";
  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.hypot(dx, dy);
  }
}

const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
p1.displayName; // undefined
p1.distance;    // undefined
p2.displayName; // undefined
p2.distance;    // undefined

console.log(Point.displayName);      // "Point"
console.log(Point.distance(p1, p2)); // 7.0710678118654755

Reference Documents:

  1. developer.mozilla.org/en-US/docs/Web/JavaSc..
  2. udemy.com/course/js-algorithms-and-data-str..