DEV Community

Cover image for Object.preventExtensions in JavaScript.
Ramu Narasinga
Ramu Narasinga

Posted on • Edited on • Originally published at thinkthroo.com

Object.preventExtensions in JavaScript.

In this article, we analyze Object.preventExtensions() usage in React source code.

Image description

Object.preventExtensions() is called when the flag hasBadMapPolyfill is false and typeof Object.preventExtensions is a function.

But what does Object.preventExtensions() do?

Object.preventExtensions

The Object.preventExtensions() static method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object). It also prevents the object’s prototype from being re-assigned.

// Example picked from MDN docs
const object1 = {};
Object.preventExtensions(object1);
try {
 Object.defineProperty(object1, 'property1', {
 value: 42,
 });
} catch (e) {
 console.log(e);
 // Expected output: 
 // TypeError: Cannot define property property1, object is not extensible
}
Enter fullscreen mode Exit fullscreen mode

Read MDN docs about Object.preventExtension()

How React uses Object.preventExtension?

There must be a good reason why extensions are not allowed to be added. I followed along the function in which this is used, FiberNode function

calls Object.preventExtension on this, but which function calls FiberNode?

[createFiberImplClass](https://github.com/facebook/react/blob/5d19e1c8d1a6c0b5cd7532d43b707191eaf105b7/packages/react-reconciler/src/ReactFiber.js#L213-L226)

calls Object.preventExtension.

Image description

This comment provides an explanation why an Object cannot be extended.

Although, I do not fully understand these functions, but found out how Object.preventExtensions can be used in real-world open-source projects.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on an interesting project. Send me an email at ramu.narasinga@gmail.com

My Github - https://github.com/ramu-narasinga
My website - https://ramunarasinga.com
My Youtube channel - https://www.youtube.com/@thinkthroo
Learning platform - https://thinkthroo.com
Codebase Architecture - https://app.thinkthroo.com/architecture
Best practices - https://app.thinkthroo.com/best-practices
Production-grade projects - https://app.thinkthroo.com/production-grade-projects

References:



AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay