Function prototype path

44 Views Asked by At

Why the third expression is false? Second and third aren't the same?

'use strict'
function func () {}
console.log(Object.getPrototypeOf(func) === Function.prototype) // true
console.log(Object.getPrototypeOf(Object.getPrototypeOf(func)) === Object.prototype) // true
console.log(Object.getPrototypeOf(Function) === Object.prototype) // false
2

There are 2 best solutions below

0
On

All functions return a prototype. Prototypes are the mechanism by which JavaScript objects inherit features from one another, not an instance of the prototype of that object. so in that third example it returns that. To clarity let's just log those:

'use strict'

function func() {}
console.log(Object.getPrototypeOf(func) === Function.prototype) // true
console.log(Object.getPrototypeOf(Object.getPrototypeOf(func)) === Object.prototype) // true
console.log(Object.getPrototypeOf(Function) === Object.prototype) // false


console.log(Object.getPrototypeOf(Object.getPrototypeOf(func))); // logs {}
console.log(func); // logs function func() {}
console.log("PT:", func.prototype); // logs - PT: {}
console.log(Object.getPrototypeOf(Function)); // = a function "function () { [native code] }"

console.log(Object.prototype); // {} = and object

1
On
// this is actually is func.__proto__ === Function.prototype    
console.log(Object.getPrototypeOf(func) === Function.prototype)
// func.__proto__.__proto__ (actually Function.prototype.__proto__) === Object.prototype
console.log(Object.getPrototypeOf(Object.getPrototypeOf(func)) === 
Object.prototype)
// Function.__proto__ !== Object.prototype, but rather Function.__proto__ 
// === Object.__proto__ and further more === Number.__proto__ === Whatever.__proto__
// because they are constructors, not instances of an object
console.log(Object.getPrototypeOf(Function) !== Object.prototype)
console.log(Object.getPrototypeOf(Function) === Object.getPrototypeOf(Object))
console.log(Function.__proto__ === Object.__proto__)