![]() |
VOOZH | about |
In JavaScript, you can create an object without a prototype using Object.create(null). This method creates a plain object that does not inherit from Object.prototype, meaning it won’t have built-in methods like toString(), hasOwnProperty(), etc., which are typically inherited from the prototype chain.
[Object: null prototype] { name: 'No Prototype Object', age: 99 }
undefined
undefined
Explanation
Comparison with a Standard Object
[Function: toString] false
A regular object created with {} inherits properties and methods from Object.prototype.
Prototype-less objects are objects that do not inherit from Object.prototype. By default, most objects in JavaScript come with built-in methods such as toString(), hasOwnProperty(), and others, which can lead to unintended conflicts when using the object as a dictionary or map.
Here are a few reasons why you might want to create an object without a prototype
We can use a prototype-less object is to create a dictionary or map where keys are string values. By avoiding inheritance, you ensure that only your defined keys are present, with no risk of conflicts from built-in properties.
[Object: null prototype] { apple: 'A fruit', car: 'A vehicle' }
undefined