How to Create Pure Objects in JavaScript
When you create new objects in JavaScript β even if it's an empty object β it will have some extra properties and methods that are inherited from the global Object class. You can check what an empty object has in JavaScript by printing it to the console:
When talking about pure objects, we are referring to objects that don't inherit anything and therefore, don't have any additional properties upfront. To create one, we can simply use the Object.create
method, and passing null
to it:
Copied to clipboard!
const pure = Object.create(null);
Passing null
to the method call sets what should be the prototype of the object, which in this case, is null
.
Resources:
π More Webtips
Master the Art of Frontend
- Access 100+ interactive lessons
- Unlimited access to hundreds of tutorials
- Prepare for technical interviews