Wednesday, February 19, 2025

JavaScript, prototypes of {} and new Object ()

Have both the same prototype: {} and new Object ()

Yes, {} and new Object() have the same prototype in JavaScript.

{} is the shorthand for creating an object literal, while new Object() is explicitly using the Object constructor.
Both {} and new Object() will have the same prototype, which is Object.prototype.
Here’s a quick comparison:
const obj1 = {};
const obj2 = new Object();

console.log(Object.getPrototypeOf(obj1) === Object.prototype); // true
console.log(Object.getPrototypeOf(obj2) === Object.prototype); // true
So, both objects inherit from Object.prototype and have the same properties and methods available through the prototype chain.

No comments:

Post a Comment

Hot Topics