What is object with null prototype in JavaScript

May 31, 2023#javascript

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

When to use object with null prototype

Some possible advantages of using an object with null prototype are:

  • It can prevent accidental property name collisions with inherited methods or properties from 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.
  • It can improve the performance of iterating over the object’s own properties using a for...in loop, since there are no inherited properties to skip.
  • It can reduce the memory footprint of the object, since it does not have any prototype chain to store.

However, there are also some drawbacks, such as:

  • It can cause compatibility issues with older browsers that do not support Object.create or __proto__.
  • It can make the object less readable and debuggable, since it does not have any useful methods like toString or hasOwnProperty.
  • It can break some third-party libraries or frameworks that rely on the presence of Object.prototype methods or properties.

How to check if an object has a null prototype

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