What is a ES6 class?

Classes are like the objects and prototypes that we’re used to working with.

There are 2 ways to define a class:

-Class Declarations

-Class Expressions

An important difference between declations and expressions is hoisting. Class expressions are hoisted, Class declaration are not.

Class declarations


class Person {
   constructor(firstName, lastName) {
   this.firstName = firstName;
   this.lastName = lastName;
  }
}

Class Expressions

unnamed:


class Person {
   constructor(firstName, lastName) {
   this.firstName = firstName;
   this.lastName = lastName;
  }
};

named:


var Person = class Person {
   constructor(firstName, lastName) {
   this.firstName = firstName;
   this.lastName = lastName;
  }
};

Classes support prototype-based inheritance, constructors, super calls, instance and static methods.

Constructors in a class are optional, if you don’t write a constructor, default constructor is used: constructor( ){ }.

You can use the class with the word new:


let p = new Person('rosa', 'de la cruz');

Leave a comment

Create a website or blog at WordPress.com

Up ↑