1
Function.prototype // function Empty() {}

How does it make sense? For example, if we will take the Number object, we can see that his prototype (Number.__proto__) is Function.prototype which includes method like apply and call. How can I use Number.apply(..) if Number's prototype is an empty function and not a regular prototype object like all other prototype? (Number prototype, String prototype, any other custom prototype are an objects. even Object.prototype is an object).

After that, how does it make sense that Object.__proto__ == Function.prototype? Object should be the highest object, how it inherits from Function.prototype when Function inherits from.. Object.prototype!

Object instanceof Function // true
Function instanceof Object // of course true
Function instanceof Function // true
Brigand
  • 84,529
  • 20
  • 165
  • 173
Luis
  • 3,257
  • 13
  • 50
  • 59

1 Answers1

1

Miklos is right, but more simply put:

Object.__proto__ == Function means that Object itself is a function, because it's the constructor. It doesn't mean that objects that inherits from Object will inherit Function. An object inherits a constructor's .prototype, not its .__proto__

In other words

function Car (){}
inst = new Car ();
// inst inherits from Car.prototype
// inst.__proto__ == Car.prototype;
// Car inherits from Function.prototype because it is a function
// Car.__proto__ == Function.prototype;

But that doesn't mean that inst inherits from Function.prototype, you can't call apply and call on it.

// This means that Everything that inherits from function will
console.log(`Function.prototype`) === function Empty() {}

Another twist

// This means that the constructor function (Object)
// inherits from `Function.prototype` That is, you can use call and apply,
// And at a lower language level, you can use () and new on it.
Object instanceof Function // true

// It doesn't mean that instances created from Object inherit 
// from Function.prototype (can't use call/apply)
(new Object()) instanceOf Function ? // false
(new Object()).apply === undefined ? // true

// This means that functions themselves are objects, everything is an object
// They have properties like hasOwnProperty and isPrototypeOf
// Not that everything that inherits from Object.prototype will also inherit
// From Function.prototype
Function instanceof Object // of course true
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Well, it makes any sense, but what about the `function Empty() {}`? EDIT: `Object.__proto__ == Function` will give you false. `Object.__proto__` refers directly to the `empty function` or via `Function.prototype`? – Luis Jul 27 '13 at 09:29
  • `Object.__proto__ == Function.prototype` just like `Car.__proto__==Function. prototype` in the example above. Think about `a=new Object ()` or `a = new Function ()`, every constructor must be a function so it will inherit from `Function.prototype` – Ruan Mendes Jul 27 '13 at 09:37
  • @Luis it refers directly to the empty function not "via `Function.prototype`. Both are independent references to the same `empty function` function object. If you reassign `Object.__proto__` it will not affect `Function.prototype` and vice versa. Mutation of course are visible for both since they reference the same object. – Esailija Jul 27 '13 at 10:34
  • 1
    @Luis Is there something that's still not answered? `.prototype` only matters for `Constructor` functions when called with `new`. That creates a new object that extends the `Constructor.prototype`, and the `Constructor` can do some initialization of the newly created object. So that when you call new on the constructor, a new object is created using something like `Object.create(Constructor.prototype)` – Ruan Mendes Jul 27 '13 at 16:57
  • That means when you create an object by using `new func()`, first it goes to func() (this is the constructor) and it returns an object + `__proto__` property which is the reference to func.prototype, right? just to be sure. In addition, `prototype` property is only inside `constructor functions` (or: inside any function, but just constructor functions really use it). – Luis Jul 27 '13 at 17:03
  • yes, `.prototype` is only used when `new MyFunc()` is called. – Ruan Mendes Jul 27 '13 at 18:13