0

Can anybody explain/elaborate on the following:

enter image description here

I have checked this on console.

Function.prototype and Function.__proto__ point to the same object. It is also the same object that is referenced by Object.__proto__. Any custom function that you write or even the built-ins one (Array,String,Number) , have their .__proto__ point to this object.

Checking Function.prototype on console returns:

ƒ () { [native code] }

Interestingly, this object's .__proto__ point to Object.prototype object.

I do understand that Object.prototype.__proto__ is null which is where the prototype chain stops.

Found the image here

pragun
  • 471
  • 1
  • 6
  • 9
  • 1
    This might help you understand - https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript & https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes – Gowtham Raj J Mar 20 '21 at 19:05
  • Every function (including `Function()` and `Object()`) inherits from `Function.prototype`, which holds function methods like `.call`/`.apply`/`.bind`, and in turn inherits from `Object.prototype`. – Bergi Mar 20 '21 at 23:15
  • @Bergi Does this not look strange to you that the Function object has its both .__proto__ and .prototype property point to a same object. String.prototype and String.__proto__ point to two different objects. Moreover, what exactly is Function.prototype object? I can't see what is inside it. It returns ƒ () { [native code] }. Is it of type function or object. You can see what is inside Function() object, Object() and Obect.prototype object. But not this one. – pragun Mar 21 '21 at 07:02
  • @GowthamRajJ, thanks, I had a look at Mozilla docs, but could not find something useful that sheds light on this. – pragun Mar 21 '21 at 07:06
  • 1
    @pragun Use `console.dir` to print it as an object - `Function.prototype` is a function for some reason. And no, `Function.prototype === Obect.getPrototypeOf(Function)` isn't strange if you consider what they mean, but it's special indeed as this only happens on a "metaclass" where the class is an instance of itself. – Bergi Mar 21 '21 at 11:16
  • 1
    The reason why `Function.prototype` remains a `Function` object is due to backwards compatibility. Basically, there is a change in ES specification regarding how should a prototype object of an object be treated. For eg, in ES5, `String.prototype` is an `String` object but in ES6, its an object of type `Object`. The exception to such changes are "prototype objects" of `Function` and `Array`. You can read more here: https://stackoverflow.com/questions/32928810/function-prototype-is-a-function – Perspicacious Aug 14 '22 at 11:48

0 Answers0