Maizere Pathak
Maizere Pathak

Reputation: 299

Function object properties

Every function object has __proto__ as their internal property. They also have prototype property. Since prototype is also an object it has a __proto__ property as well. My question is, do both the __proto__ property inside the prototype and in the function object point to Function.prototype?

Upvotes: 1

Views: 96

Answers (4)

Kos
Kos

Reputation: 72261

A function's attribute named prototype is a normal object which becomes the prototype for objects created when using that function as a constructor.

A function's actual prototype (accessible via __proto__) is an object called Function.prototype, which in order descends from Object.prototype.

Diagram

Upvotes: 1

Gabriel Gartz
Gabriel Gartz

Reputation: 2870

The __proto__ property points to the constructor from the function when you use the new operator.

If your constructor is a instanceof Function, all the properties and methods in the Function.proptotype object will be accessible by the instance, and will use the same as context.

Some browsers don't implement access to __proto__ object, so if you wan't to use, will lose compatibility.

More info in the MDN docs: https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Object/proto

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816422

No. A function's prototype property (i.e. SomeFunc.prototype) is a normal object and so its internal __proto__ property points to Object.prototype.

Simple way to test it:

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

Only functions inherit from Function.prototype, no other objects.

Upvotes: 1

LetterEh
LetterEh

Reputation: 26696

No.

The hidden __proto__ property, which shouldn't really be used directly by JS programmers is meant as the glue to tie an object to its prototype chain.

As such:

function x () {}

x.__proto__; // is a prototypical function, leading back to `Function.prototype`

However, prototype is just an object, which gets assigned to the __proto__ property of whatever you're creating with new, inside the function.

As it's just a regular object, __proto__ will eventually lead back to Object.prototype.

Upvotes: 0

Related Questions