JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - Object Prototypes

In JavaScript, every object has a prototype. A prototype is essentially a template object from which other objects inherit methods and properties. Prototypes are a fundamental part of JavaScript's prototypal inheritance model and provide a powerful mechanism for reusing code and linking objects.

The prototype is itself an object, and this prototypal inheritance chain continues until it reaches null as the prototype of the last link. The prototype object contains methods and properties that are common to all instances of the object type, allowing them to be shared and reused across each instance efficiently.

This prototype system underlies JavaScript's dynamic nature, allowing for flexible and powerful object-oriented programming. For example, all JavaScript objects created from array literals inherit from Array.prototype, giving them access to methods like map, filter, and reduce without the need for each array to define these methods.

Understanding and utilizing prototypes is crucial for efficient JavaScript programming, as it not only aids in reducing redundancy but also enhances performance by keeping memory usage minimal.

Understanding Prototypes with Objects

Let’s start with understanding the prototype of a basic object.

Example

The below example creates a simple object and demonstrates how to access its prototype and how it inherits properties from its prototype chain.

Javascript
Javascript

. . . .

Explanation:

  • An object animal is created with a property type set to 'Mammal'.
  • Object.getPrototypeOf(animal) is used to access the prototype of the animal object. In JavaScript, all plain objects inherit from Object.prototype by default.
  • We then log the prototype of animal, which shows it inherits from Object.prototype.
  • We check if animalPrototype has its own toString method using hasOwnProperty('toString'), which returns true because toString is in the object prototype.
  • The type of toString is checked, confirming it's a function, demonstrating inheritance from the prototype.
  • Finally, animal.toString() is called, which executes the toString method inherited from Object.prototype, showing polymorphic behavior where animal can use methods defined up the prototype chain.

Adding Methods to an Object's Prototype

Next, we'll add a method to the animal object's prototype and see how it can be accessed by the animal object.

Example

The below example adds a custom method to the prototype of the animal object and accesses it using the animal instance.

Javascript
Javascript

. . . .

Explanation:

  • A method speak is added to animal's prototype (which is Object.prototype in this context). This method logs a message including the type property of the object calling it.
  • When animal.speak() is called, JavaScript looks up the prototype chain, finds the speak method, and executes it using animal as this, demonstrating how methods added to a prototype are accessible to all objects that inherit from that prototype.

Enhancing Functionality of Built-in Objects with Prototypes

One practical use of prototypes in JavaScript is enhancing the functionality of built-in JavaScript objects like Arrays, Strings, or even Functions. By adding methods to these objects' prototypes, you can extend their capabilities in ways that can be reused across your entire application.

Example: Adding a Sum Method to the Array Prototype

Here, we'll add a new method to the Array prototype to provide custom functionality for all array objects, specifically a method to find the sum of numerical array elements.

Javascript
Javascript

. . . .

Explanation:

  • Adding Method to Prototype: The sum method is added to Array.prototype. This method uses the reduce function, which is a built-in method of the Array object that reduces the array to a single value. Here, it sums up all the elements starting from an initial value of 0.
  • Creating an Array Instance: An array named numbers is defined with the elements [1, 2, 3, 4, 5].
  • Using the Extended Method: The sum method is called on the numbers array. Since this method has been added to Array.prototype, all arrays inherit this method. The method calculates the sum of the array elements and outputs 15.

Benefits of Prototype Extension

  • Reusability: Methods added to prototypes are automatically available to all instances of the object, promoting code reusability and reducing redundancy.
  • Efficiency: Extending prototypes can be more memory-efficient than adding methods directly to object instances, especially when there are many instances that will use the same method.
  • Customization: It allows developers to tailor existing JavaScript objects to their specific needs, making built-in objects more versatile.

Caution

While extending native prototypes can be powerful, it should be done with caution:

  • Conflicts: If third-party libraries are used, they might also modify the same prototype, leading to conflicts.
  • Maintainability: Over-extending native prototypes or changing their behavior significantly can make the code harder for other developers to understand and maintain.
  • Future Compatibility: Future versions of JavaScript might add methods that clash with your extensions.

Using prototypes to extend the functionality of built-in JavaScript objects offers a robust way to enhance and standardize functionality across an application. However, it's important to consider the potential pitfalls of this approach. Properly used, prototype extension is a powerful feature that can simplify code and improve performance in large-scale applications.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next