Welcome to the exciting realm of constructors in JavaScript! Constructors are special functions that serve as blueprints for creating objects. They define the properties and behaviors that all instances (objects) created from them will share. This chapter delves into the fundamentals and advanced concepts of constructors, empowering you to craft well-structured and reusable objects in your JavaScript code.
new
Keywordnew
keyword is the cornerstone of constructor usage. When used with a constructor function, it creates a new object, initializes it with the properties and methods defined within the constructor, and returns a reference to the newly created object.
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const person1 = new Person("Alice", "Smith");
console.log(person1); // Output: Person { firstName: "Alice", lastName: "Smith" }
Person
that takes two arguments: firstName
and lastName
.this
refers to the newly created object. We use this
to assign the function arguments to the object’s properties (this.firstName
and this.lastName
).new Person("Alice", "Smith")
, a new object is created with the provided values for firstName
and lastName
. The console.log
statement displays the newly created object person1
.
function Car(model, year) {
this.model = model;
this.year = year;
this.getDetails = function() {
return `Model: ${this.model}, Year: ${this.year}`;
};
}
const car1 = new Car("Camry", 2020);
console.log(car1.model); // Output: Camry
console.log(car1.getDetails()); // Output: Model: Camry, Year: 2020
Car
constructor defines two properties (model
and year
) and a method getDetails
that returns a string with car details.Car
object (car1
), it inherits these properties and the method.In JavaScript, objects inherit properties and methods from their prototype. The constructor function itself has a property named prototype
that serves as the prototype for objects created using new
function Animal(species) {
this.species = species;
}
Animal.prototype.makeSound = function() {
console.log("Generic animal sound");
};
const dog = new Animal("Dog");
dog.makeSound(); // Output: Generic animal sound
Animal
constructor with a species
property.makeSound
method to the Animal.prototype
. This method becomes accessible to all objects created using Animal
.dog
object, it inherits the makeSound
method from the Animal
prototype, even though it’s not defined directly in the Animal
constructor.
function Dog(species) {
this.species = species;
}
Dog.prototype.makeSound = function() {
console.log("Woof!");
};
const dog = new Dog("Dog");
dog.makeSound(); // Output: Woof!
Dog
constructor inherits the makeSound
method from Animal.prototype
.makeSound
method within the Dog
constructor that specifically prints “Woof!”.dog
object, it uses the makeSound
method defined directly in the Dog
constructor, overriding the inherited one.Constructors empower you to build reusable object blueprints in JavaScript. They provide structure, define initial properties and methods, and enable inheritance through prototypes. By understanding these concepts, you can create maintainable and efficient object-oriented applications Happy coding !❤️