Monkey Patching in JavaScript
Tags: javascript, webdev, learning, programming
Published: 2024-05-27 19:08:46

## **What is Monkey Patching 🐵**

The term “**monkey patching**” is borrowed from the world of software development, where it refers to the practice of modifying or extending code at runtime. In JavaScript, it allows developers to alter or extend the behavior of existing objects or classes without directly modifying their source code.

In simple term's it is : <u>_**Monkey patching**_ is a technique to add, modify, or suppress the default behavior of a piece of code at runtime without changing its original source code.</u>

Monkey patching involves making changes to existing code, typically by adding new methods or properties to objects or classes, or by modifying existing ones. This can be done at runtime, allowing developers to augment functionality without altering the original code base. Monkey patching is often used to fix bugs, add missing features, or adapt third-party libraries to better suit specific requirements.

**_Let's see a basic example to understand this concept_**



![basic-code-example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4yexva04c1jposq7xea.png)

<u>**Pros of Monkey Patching**</u>

1. Monkey patching allows developers to extend or modify existing code without directly altering its source, providing flexibility in adapting libraries or frameworks to specific requirements.

2. Developers can experiment with new features or enhancements without committing to permanent changes in the code base.

3. It enables quick fixes to issues or bugs in third-party code without waiting for official updates or patches.

<u>**Cons of Monkey Patching**</u>

1. Overuse of monkey patching can lead to code base complexity and reduced readability, making it harder to understand and maintain.

2. Modifying existing code at runtime can introduce unintended side effects or conflicts, especially in large or collaborative projects.
3. **Global Scope Pollution**: Global scope pollution refers to a situation where too many variables, functions, or objects are introduced into the global scope. This makes it cluttered and can potentially lead to unintended behaviors. Monkey patching can introduce global scope pollution by adding or modifying properties and methods on built-in prototypes or objects. This can lead to naming conflicts and bugs, especially when using multiple libraries or codebases.

**Traditional Monkey Patching and Why It Is it is Dangerous**


![traditional-monkey-patching](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1zoy7ll6h30zjemtfwx.png)

- The author of `Array` could add a different implementation of `logAllMembers`, which could break your code which relies on your implementation.
- Elsewhere in your codebase, someone could add a different implementation of `logAllMembers`, leading to fragile code.
- Elsewhere in your codebase, it will be hard to tell whether `Array` has been patched or not, so other programmers (or you) could accidentally depend on `Array` being patched or not-patched.

## **Conclusion**

**Monkey patching** is a powerful technique for extending or modifying existing code dynamically. However, it comes with considerations like understanding the prototype chain, avoiding global scope pollution. While implementing monkey patching, you must exercise caution, document changes, and thoroughly test patches to mitigate compatibility issues and security risks.