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?
-
@Felix Kling haha i m relly weak in english .Maizere Pathak– Maizere Pathak2013年01月27日 18:27:20 +00:00Commented Jan 27, 2013 at 18:27
4 Answers 4
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.
Comments
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
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.
Comments
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