From the javascript console in Chrome:
> function Person(name){this.name=name;}
undefined
At this point, Person.prototype should be an empty Object according to the Javascript specs.
Let’s assign it:
> p=Person.prototype
> Person
Note that that > Person is clickable and it expands to:
constructor: function Person(name){this.name=name;}
__proto__: Object
But… wasn’t it meant to be an empty object? What is all the extra stuff?
If you do an alert:
alert(p)
You get [object Object]. Why, when you type it in the Chrome console, does it come out with > Person which expands? Wasn’t it meant to be an empty object?
Thank you!
No, the
prototypealways has theconstructorproperty which points to the function it is the prototype of. And of course it inherits from an object too, that is the internal__proto__property.It is defined in ECMAScript 5 Section 13.2, Creating Function Objects:
This means nothing else than:
Create a new empty object called proto (16). Define the property
constructoron that object and set the value to F (the function itself) (17). Then define the propertyprototypeon the function F and set its value to proto.If you
alertan object, then the object is converted to a string. The default behaviour is to convert an object to the[object Object]string, unless the "special"toStringmethod is overridden.The Chrome console lists these properties because it is meant for debugging, so you need information.
[object Object]is not very informative.FWIW, an empty object looks like this:
You can also see the internal
__proto__property here. An empty object always inherits some default properties, but it does not have own properties.