Skip to content

Latest commit

 

History

History
136 lines (98 loc) · 3.59 KB

Constructors.md

File metadata and controls

136 lines (98 loc) · 3.59 KB

Constructors in JavaScript with ES6+

Table of Contents

  1. Introduction to Constructors
  2. Constructor Functions
  3. ES6+ Class Syntax
  4. Inheritance
  5. Static Methods
  6. Private Fields and Methods

Introduction to Constructors

Constructors are special functions used to create and initialize objects created within a class. In JavaScript, constructors can be defined using constructor functions or ES6 class syntax. Constructors are essential for creating multiple instances of objects with shared properties and methods.

Constructor Functions

Constructor functions are defined using the function keyword and are invoked with the new keyword to create new instances of objects.

Example

function Person(name, age) {
  this.name = name;
  this.age = age;
}

Person.prototype.greet = function() {
  console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
};

const person = new Person('Alice', 30);
person.greet(); // 'Hello, my name is Alice and I am 30 years old.'

ES6+ Class Syntax

ES6 introduced class syntax, which provides a more structured and readable way to define constructors and methods.

ES6+ Example

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

const person = new Person('Alice', 30);
person.greet(); // 'Hello, my name is Alice and I am 30 years old.'

Inheritance

Inheritance allows you to create a new class that is a modified version of an existing class. ES6+ provides the extends keyword to implement inheritance.

Inheritance Example

class Student extends Person {
  constructor(name, age, grade) {
    super(name, age); // Calls the parent class constructor
    this.grade = grade;
  }

  study() {
    console.log(`${this.name} is studying for grade ${this.grade}.`);
  }
}

const student = new Student('Bob', 20, 'A');
student.greet(); // 'Hello, my name is Bob and I am 20 years old.'
student.study(); // 'Bob is studying for grade A.'

Static Methods

Static methods are methods that are called on the class itself, not on instances of the class. They are defined using the static keyword.

Static Methods Example

class MathHelper {
  static square(num) {
    return num * num;
  }
}

console.log(MathHelper.square(5)); // 25

Private Fields and Methods

ES6+ introduced private fields and methods using the # symbol. These are not accessible outside the class.

Private Fields and Methods Example

class Counter {
  #count = 0; // Private field

  increment() {
    this.#count++;
  }

  getCount() {
    return this.#count;
  }
}

const counter = new Counter();
counter.increment();
console.log(counter.getCount()); // 1
console.log(counter.#count); // SyntaxError: Private field '#count' must be declared in an enclosing class

Tip

Use constructor functions or ES6 class syntax to create and initialize objects. Leverage inheritance to create specialized classes from base classes. Use static methods for utility functions that do not require instance-specific data. Use private fields and methods to encapsulate implementation details.

[EOF]