Skip to content

Latest commit

 

History

History
55 lines (37 loc) · 2.72 KB

module-2.1-constructors-and-prototypes.md

File metadata and controls

55 lines (37 loc) · 2.72 KB

2.1. Constructors and Prototypes

Object-Oriented Programming (OOP) in JavaScript often begins with understanding Constructors and Prototypes. These concepts are fundamental to creating and managing objects.

Constructors in JavaScript

In JavaScript, constructors are functions used to create and initialize objects. They serve as templates for creating objects with shared properties and methods. Here's an example of a simple constructor for creating Person objects:

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

const john = new Person('John', 30);
console.log(john.name); // Output: John

In this example, we define a Person constructor that takes name and age as parameters. When we create a new Person object using new, it initializes the object's properties based on the constructor.

Prototypes and Inheritance

Prototypes allow objects to inherit properties and methods from other objects. Understanding prototypes is crucial for creating efficient and maintainable code in JavaScript.

// Creating a prototype method
Person.prototype.greet = function () {
  console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
};

john.greet(); // Output: Hello, my name is John and I'm 30 years old.

In this example, we add a greet method to the Person prototype, which all Person objects can access. This method is shared among all instances of Person, making it more memory-efficient.

Constructor Functions and new

  • Constructor functions, like Person in the example, are named with capital letters by convention.
  • The new keyword is used to create instances of objects based on the constructor.
  • Inside the constructor, this refers to the new instance being created.
  • Properties and methods specific to an object are set using this.propertyName inside the constructor.

Prototype Chain and Inheritance

  • When you add methods or properties to a constructor's prototype, they are shared among all objects created from that constructor.
  • Objects can access properties and methods defined in their constructor's prototype, creating an inheritance mechanism.
  • This promotes code reusability and efficient memory usage.

Best Practices

  • Use constructor functions for creating multiple instances of objects with shared properties and methods.
  • Place methods shared among instances in the constructor's prototype to optimize memory usage.
  • Follow naming conventions, such as capitalizing the first letter of constructor functions, for clarity.

Understanding Constructors and Prototypes is foundational to mastering Object-Oriented Programming in JavaScript. They enable you to create organized, efficient, and maintainable code.