Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is base object in javascript

Tags:

javascript

https://github.com/lydiahallie/javascript-questions#14-all-object-have-prototypes All objects have prototypes, except for the base object. What is base object

like image 553
gxr Avatar asked Dec 31 '22 21:12

gxr


1 Answers

The base object is Object.prototype:

The Object.prototype is a property of the Object constructor. And it is also the end of a prototype chain.

console.log(Object.getPrototypeOf(Object.prototype));

Most objects inherit from some prototype, which may inherit from some other prototype, eventually ending at Object.prototype.

console.log(
  Object.getPrototypeOf(Function.prototype) === Object.prototype,
  Object.getPrototypeOf(Number.prototype) === Object.prototype,
  Object.getPrototypeOf(Object.getPrototypeOf(5)) === Object.prototype
);

That said, the text in your link isn't entirely accurate - it's possible to create objects which do not ultimately inherit from Object.prototype, eg:

const obj = Object.create(null);
console.log(Object.getPrototypeOf(obj));

This can be done to avoid (probably unusual) name collisions for Object.prototype methods, which can cause bugs.

like image 92
CertainPerformance Avatar answered Jan 08 '23 02:01

CertainPerformance