When would a [[Prototype]] refers to a constructor [[Prototype]]: Constructor

127 views Asked by At

While learning prototypical inheritance, I have seen the prototype referring to Array, Object or empty Function.

https://i.stack.imgur.com/cdiky.png

I am new to JavaScript programming (prototypical inheritance world) and when debugging SAPUI5 framework I have noticed the prototype is pointing to a constructor.

enter image description here

Could you please explain me in which scenario [[Prototype]] points to a constructor. Can [[Prototype]] point to any other apart from these 4 ( Array, Object, Empty Function and Constructor )?

More Info: If the function is a constructor function it should have a prototype property at the same level where it displays the [[Prototype]]

Constructor Function Example with prototype property and [[Prototype]]

prototype property missing

2

There are 2 answers

13
Bergi On BEST ANSWER

A [[prototype]] can point to any object (or null). What you're asking though is how that object is displayed in the devtools - see How does DevTools determine an object's constructor's name? and Javascript Debugging in Chrome - object name? - which uses the name of the function that constructed the object. It means that the prototype is an object or an array or a function, not actually the global Object/Array/Function. When you define your own classes, it'll be the name of that class, which can be anything. In your exampe, SAPUI5 appears to have defined its class(es) via function constructor() {}, which is unusual but doesn't matter.

2
Sean Morris On

constructor is just the name of the function that generated the object. The Array object's constructor happens to be named Array because Javascript's inheritance model was prototypical, historically. Most Javascript builtins will follow this pattern.

Objects from classes implemented in the modern ES6 class based notation will have a constructor that is actually named constructor.