What are the differences between ES6 class and ES5 function constructors?

JavaScript is a powerful and versatile programming language that has undergone many changes and updates over the years. One of the most notable changes was the introduction of the class syntax in ECMAScript 6 (ES6), which provides a more familiar and intuitive way to create objects and organize code. In this blog post, we will explore the differences between the ES6 class syntax and the traditional function constructor pattern in ES5.

Differences

The first major difference between the two is the syntax. In ES5, function constructors were used to create objects and their methods. A function constructor is a function that is used to create an object, and it is invoked with the new keyword. The following is an example of a function constructor in ES5:

function Person(name) {
    this.name = name;
    this.greet = function() {
        console.log(`Hello, my name is ${this.name}`);
    }
}
let person = new Person("John");
person.greet();

In contrast, the ES6 class syntax provides a more familiar and intuitive way to create objects and organize code, similar to other object-oriented languages such as Java or C#. The following is an example of a class in ES6:

class Person {
    constructor(name) {
        this.name = name;
    }
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
}
let person = new Person("John");
person.greet();

As you can see, the class syntax is more concise and easier to read. It also makes it clear that the greet method is a part of the Person class, whereas in the function constructor pattern it is just a property of the object.

Method definition

Another major difference between the two is the way that methods are defined and shared among objects. In the function constructor pattern, each object created with the constructor function has its own copy of the methods. This means that if you have multiple objects created with the same function constructor, each object will have its own copy of the methods, which can lead to memory inefficiency.

function Person(name) {
    this.name = name;
    this.greet = function() {
        console.log(`Hello, my name is ${this.name}`);
    }
}
let person1 = new Person("John");
let person2 = new Person("Jane");
console.log(person1.greet === person2.greet); // false

On the other hand, in the class syntax, methods are shared among objects. This means that if you have multiple objects created with the same class, they will all share the same methods, which can lead to memory efficiency.

class Person {
    constructor(name) {
        this.name = name;
    }
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
}
let person1 = new Person("John");
let person2 = new Person("Jane");
console.log(person1.greet === person2.greet); // true

Extend keyword

Another difference is that the class syntax allows for the use of inheritance and the extends keyword. This allows for the creation of a base class that can be inherited by other classes, making it possible to share common methods and properties among related

 classes. The extends keyword is used to create a subclass that inherits the properties and methods of the base class.

class Person {
    constructor(name) {
        this.name = name;
    }
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
}

class Student extends Person {
    constructor(name, major) {
        super(name);
        this.major = major;
    }
    study() {
        console.log(`Studying ${this.major}`);
    }
}

let student = new Student("John", "Computer Science");
student.greet(); // "Hello, my name is John"
student.study(); // "Studying Computer Science"

In this example, the Student class is a subclass of the Person class, and it inherits the greet method from the Person class. Additionally, it has its own study method.

On the other hand, in the function constructor pattern, inheritance is achieved by using the Object.create() method, which creates a new object with the prototype set to an existing object. This can be more complex and harder to understand than the class syntax.

function Person(name) {
    this.name = name;
    this.greet = function() {
        console.log(`Hello, my name is ${this.name}`);
    }
}

function Student(name, major) {
    Person.call(this, name);
    this.major = major;
    this.study = function() {
        console.log(`Studying ${this.major}`);
    }
}

Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

let student = new Student("John", "Computer Science");
student.greet(); // "Hello, my name is John"
student.study(); // "Studying Computer Science"

In addition, the class syntax also allows for the use of the static keyword. This is used to define a method that is associated with the class itself, rather than with an instance of the class. This can be useful for utility methods that do not need access to the instance's properties.

class Person {
    constructor(name) {
        this.name = name;
    }
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
    static createPerson(name) {
        return new Person(name);
    }
}

let person = Person.createPerson("John");
person.greet(); // "Hello, my name is John"

In conclusion, the ES6 class syntax and the traditional function constructor pattern in ES5 are two different ways to create objects and organize code in JavaScript. The class syntax provides a more familiar and intuitive way to create objects and organize code, similar to other object-oriented languages such as Java or C#. It also allows for the use of inheritance and the extends keyword, and the static keyword. On the other hand, the function constructor pattern is more complex and harder to understand. It's important to note that both patterns can be used in any JavaScript codebase, but the class syntax is recommended for new projects because

Additional resources
  • Frontend web development courses

    Beginner-friendly courses focusing on HTML, CSS, and JavaScript.

    View Courses
  • Frontend web development projects

    Beginner-friendly projects focusing on HTML, CSS, and JavaScript.

    View Projects
  • Free website templates

    Collection of free, high-quality website templates for your next project.

    View Templates