Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Classes in Javascript

Introduction

  1. Classes are a template for creating objects
  2. They encapsulate data with code to work on that data
  3. The class syntax has two components: class expressions and class declarations
Key points
  1. Class declarations are not hoisted
  2. Trying to access a class before declaring, gives a ReferenceError
Class declarations
  1. One way to define a class is using a class declaration
  2. To declare a class, you use the class keyword with the name of the class
class Rectangle {\n
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}
Class expressions
  1. We can also use class expressions to define our classes
  2. The name given to a named class expression is the class name
let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle"
Class body
  1. The body of a class is the part that is in curly brackets {}
  2. This is where you define class members, such as methods or constructor
Constructor
  1. It is a special method for creating of class
  2. It initializes an object created with a class
  3. the super keyword is used to call the constructor of the superclass
Methods
  1. Methods can be added after the constructor method
  2. Methods can be called with class instance
class Person{
constructor(name){
    this.name = name;
  } 
  
  getName(){
    return this.name;
  }
}

let john = new Person('John');\n
console.log(john.getName());\n
// John
Static methods and properties
  1. The static keyword defines a static method of a class
  2. Static methods are used to define fixed configuration
  3. Static methods are called without instantiating their class
  4. Cannot be called through a class instance
The static method with an instance
  1. Calling a static method with instance gives an error
  2. Error: john.getDepartment is not a function
class Person{
constructor(name){
    this.name = name;
  } 
  static department = 'Software';
  static getDepartment = () =>{
    return this.department;
  }
  getName(){
    return this.name;
  }
}

let john = new Person('John');
console.log(john.getName());
console.log(john.getDepartment());
// john.getDepartment is not a function
The static method with class name
A static method can be called with class name
class Person{
constructor(name){
    this.name = name;
  } 
  static department = 'Software';
  static getDepartment = () =>{
    return this.department;
  }
  getName(){
    return this.name;
  }
}

  let john = new Person('John');
  console.log(john.getName());
  console.log(Person.getDepartment());

//John
//Software
Public field declarations
  1. Public fields can be defined at top of the class body
  2. Public fields can be accessed from outside of a class
class Person{
salary = 8000;
constructor(name){
    this.name = name;
  }
}

let john = new Person('John');
console.log(john.salary);
// 8000
Private field declarations
  1. Private fields can be defined at top of the class body
  2. Private fields cannot be accessed from outside of a class
class Person{
#salary = 8000;
constructor(name){
    this.name = name;
  }
}

let john = new Person('John');
console.log(john.salary);
// undefined
Private field declarations
  1. Private fields can be defined at top of the class body
  2. Private fields cannot be accessed from outside of a class
class Person{
#salary = 8000;
constructor(name){
    this.name = name;
  }
}

let john = new Person('John');
console.log(john.salary);
// undefined
Creating subclass
  1. The extends keyword is used to create a class as a child of another class
  2. If there is a constructor present in the subclass, it needs to first call super() before using this
class Person{
salary = 8000;
constructor(name){
    this.name = name;
  }
  getSalary(){
    return this.salary;
  }
}

class Employee extends Person{
constructor(salary){
  super();
    this.salary = salary;
  }
  getSalary(){
    return this.salary;
  }
}

let john = new Person('John');
let stark = new Employee(6000);
console.log(john.getSalary());
console.log(stark.getSalary());
//8000
//6000
Superclass methods in the subclass
Superclass methods can be accessed with the keyword super
class Person{
salary = 8000;
constructor(name){
    this.name = name;
  }
  getSalary(){
    return this.salary;
  }
}

class Employee extends Person{
constructor(salary){
  super();
  }
  getSalary(){
  let sal = super.getSalary();
    return sal;
  }
}

let john = new Person('John');
let stark = new Employee(6000);
console.log(john.getSalary());
console.log(stark.getSalary());
//8000
//8000