In JavaScript, an object with a null prototype refers to an object that does not inherit any properties or methods from its prototype chain, which is a mechanism that allows objects to inherit properties and methods from other objects.
Objects with null prototypes are often used to create plain dictionaries or maps where you don’t want any inherited properties or methods. They provide a clean and efficient way to store key-value pairs without the overhead of inheriting unnecessary functionality.
By default, when you create a new object in JavaScript using object literal syntax {}
, the newly created object inherits properties and methods from the Object prototype. This means that the object will have access to common methods like toString()
or hasOwnProperty()
.
const obj = {};
console.log(obj.toString); // [Function: toString]
console.log(obj.hasOwnProperty); // [Function: hasOwnProperty]
console.log(obj.valueOf); // [Function: valueOf]
console.log(obj.constructor); // [Function: Object]
console.log(obj.__proto__); // [Object: null prototype] {}
However, if you create an object with a null prototype, it means that the object doesn’t inherit any properties or methods from the Object prototype or any other prototype. You can create an object with a null prototype using the Object.create()
method and passing null
as the prototype parameter.
const obj = Object.create(null);
console.log(obj.toString); // undefined
console.log(obj.hasOwnProperty); // undefined
console.log(obj.valueOf); // undefined
console.log(obj.constructor); // undefined
console.log(obj.__proto__); // undefined
You can add properties or methods to an object with null prototype, just like any other object. For example:
const obj = Object.create(null);
obj.name = "Alice"; // add a property
obj.greet = function() { // add a method
console.log("Hello, I'm " + this.name);
};
obj.greet(); // Hello, I'm Alice
console.log(obj);
// [Object: null prototype] {
// name: 'Alice',
// greet: [Function (anonymous)]
// }
You can use Object.assign
to copy an object with null prototype, as long as the source object is not null
or undefined
. For example:
const obj = Object.create(null);
obj.name = "Alice"; // add a property
obj.greet = function() { // add a method
console.log("Hello, I'm " + this.name);
};
const copy = Object.assign({}, obj); // copy the object
console.log(Object.getPrototypeOf(copy));
//=> [Object: null prototype] {}
copy.greet();
//=> Hello, I'm Alice
Some possible advantages of using an object with null prototype are:
Object.prototype
. For example, if you use an object as a map to store key-value pairs, you might overwrite or access the built-in properties like toString
or constructor
unintentionally.for...in
loop, since there are no inherited properties to skip.However, there are also some drawbacks, such as:
Object.create
or __proto__
.toString
or hasOwnProperty
.Object.prototype
methods or properties.One way to check if an object has a null prototype is to use the Object.getPrototypeOf
method, which returns the prototype of the specified object. If the object has a null prototype, this method will return null
. For example:
const obj = Object.create(null);
console.log(Object.getPrototypeOf(obj)); // null
Another way to check if an object has a null prototype is to use the Object.prototype.isPrototypeOf
method, which checks if an object exists in another object’s prototype chain. If the object has a null prototype, this method will return false
for any other object. For example:
const obj = Object.create(null);
console.log(Object.prototype.isPrototypeOf(obj)); // false