Open In App

Method - JavaScript Design Pattern

Last Updated : 08 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

A Method is a JavaScript design pattern where objects share a common object, which contains shared methods. The method allows you to reuse the properties and methods of the object, and also add new ones as needed. The method is useful for performance optimization, as it avoids creating new functions for each object instance. It also supports prototypal inheritance, which is a different way of implementing object-oriented programming in JavaScript.

Syntax:

function MyObject() {
this.property1 = 'Value 1';
this.property2 = 'Value 2';
}
MyObject..method1 = function() {
// Method 1 implementation
};
MyObject..method2 = function() {
// Method 2 implementation
};

Example: In this example, a person object is created with a greet method, which prints a greeting with the name property. Then, a new object data is created, extending the person. By setting the name property to 'Rohan' and invoking data.greet(), it logs "Hello, my name is Rohan," demonstrating -based inheritance for shared behavior in JavaScript objects.

JavaScript
// Create a  object with a method
const person = {
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    },
};

// Create a new object and extend its 
const data = Object.create(person);
data.name = 'Rohan';

// Invoke the method on the extended object
data.greet(); 

Output
Hello, my name is Rohan

Example: In this example, defines a Car constructor function that creates car objects with properties like model, year, and miles. The toString method is added to each car's to display car information, and two car instances are logged to the console.

JavaScript
function Car(model, year, miles) {
    this.model = model;
    this.year = year;
    this.miles = miles;
}
Car..toString = function () {
    return this.model + " has done " + this.miles + " miles";
};
let civic = new Car("Honda Civic", 2009, 20000);
let mondeo = new Car("Ford Mondeo", 2010, 5000);
console.log(civic.toString()); 
console.log(mondeo.toString()); 

Output:

Honda Civic has done 20000 miles
Ford Mondeo has done 5000 miles

The method in JavaScript design pattern is one of the core features of the language, and it is widely used in many libraries and frameworks.

Advantages:

  • Efficient Object Creation: Objects can be created by copying existing ones, saving time and memory.
  • Dynamic Updates: Changes made to a are reflected in all instances, allowing for dynamic updates.
  • Code Reusability: Multiple objects can share the same , reducing code duplication.
  • Easy Object Initialization: Objects can be initialized without specifying all properties and methods during creation.
  • Structured Development: Encourages a structured approach to object creation and inheritance, promoting best coding practices.

Disadvantages:

  • Complexity: Managing and updating s and their relationships can become complex as the application grows.
  • Confusion: If not implemented carefully, it can lead to confusion and unintended consequences when modifying s.
  • Potential for Overwriting: Modifying the can unintentionally affect all instances, causing unexpected behaviour.
  • Performance Overhead: Cloning objects from s can introduce some performance overhead, especially when dealing with deeply nested structures.
  • Limited Privacy: s do not provide the same level of data encapsulation and privacy as some other design patterns, potentially exposing object properties and methods unintentionally.

Next Article

Similar Reads