- Introduction to Constructors
- Constructor Functions
- ES6+ Class Syntax
- Inheritance
- Static Methods
- Private Fields and Methods
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 are defined using the function
keyword and are invoked with the new
keyword to create new instances of objects.
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 introduced class syntax, which provides a more structured and readable way to define constructors and methods.
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 allows you to create a new class that is a modified version of an existing class. ES6+ provides the extends keyword to implement inheritance.
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 are methods that are called on the class itself, not on instances of the class. They are defined using the static keyword.
class MathHelper {
static square(num) {
return num * num;
}
}
console.log(MathHelper.square(5)); // 25
ES6+ introduced private fields and methods using the # symbol. These are not accessible outside the class.
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]