is function a object in javascript?
Yes.
Does foo has a property named proto pointing to the Function 's prototype?
Not named proto
, no. This is a bit of a confusing area:
All objects have an underlying prototype object they inherit from. From ES5 onward, you can get a reference to that object via Object.getPrototypeOf
(e.g., var p = Object.getPrototypeOf(blah)
gives you a reference to blah
's prototype). Some engines (including Firefox's SpiderMonkey) also make underlying prototype available via the pseudo-property __proto__
, and that will be in the spec as of ES6, but officially only when JavaScript is used in browsers.
Separately, all normal functions have a property called prototype
, which is not that function's underyling prototype (see above). Instead, it's the object that will get assigned as the underlying prototype of objects created using that function as a constructor via the new
keyword.
Since this is a bit confusing, let's draw a picture. Suppose we have this code:
function Foo() {
this.name = "xx";
}
var f = new Foo();
(Since I used it as a constructor, I've used the name Foo
rather than foo
to stick to conventions.)
Here's what we now have in memory:
+------------+
| Function |
| (function) |
+------------+ +-----------+
| __proto__ |----+->| (object) | This is the object assigned to functions
| prototype |----/ +-----------+ as their underyling prototype
+------------+ | | call |
| | apply |
| | bind |
| | ... |
| +-----------+
+------------+ |
| Foo | |
| (function) | |
+------------+ |
| __proto__ |----+
| | +----------+
| prototype |----+---->| (object) | This is the object assigned to
+------------+ | +----------+ objects created via `new Foo`
| | (blank) |
| +----------+
+-----------+ |
| f | |
| (object) | |
+-----------+ |
| __proto__ |-----+
| name: xx |
+-----------+
(I've left off the references to Object.property
above to keep things simpler.)
How can I see this property.
See above, either __proto__
(on only some engines for now, will be standard in ES6) or via Object.getPrototypeOf
(ES5).
Example:
// Shows you the object referenced by Function.prototype,
// which is `foo`'s underyling prototype
// ONLY WORKS ON SOME ENGINES for now (including Firefox's SpiderMonkey)
console.log(foo.__proto__);
// Shows you the object referenced by foo.prototype
console.log(foo.prototype);