0

I may not reading chromes object display as I should :

I have this code :

function Foo(){};
var g= new Foo();

Also - ( console code) :

>>Foo.prototype //  Foo {}

Question :

Does it say its type is instance of Foo ? or just a plain regular object ?

p.s. - I've heard that prototype is a regular object. so the Foo word in Foo{} makes me think it is an instance of Foo

Why am I asking ?

Because running this :

Foo.prototype.__proto__

shows Object {} and not its instance constructor prototype which is : Foo.prototype ...

Additional info :

Related question of mine ( which doesn't talks about the way chrome displays objects :

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

3

Foo.prototype has a property constructor, which is defined as Foo.prototype.constructor = Foo; and that's why the console shows it like that

That's the consequence of Foo.prototype having a constructor property. Each prototype of a constructor function has a constructor property which points back to that function, e.g.

Foo.prototype.constructor = Foo;

Now, it looks like in order to determine which constructor function an object is an instance of, Chrome's console looks at the constructor property of an object and then assumes that the object is an instance of that function.
This works in almost all cases, but not for prototype objects, because they have explicitly set a constructor property.

Example:

> function Foo() {}
undefined
> Foo.prototype.constructor = function xyz() {}
function xyz() {}
> Foo.prototype
xyz {}

Another, simpler example:

> var obj = {};
> obj
Object {}
> obj.constructor = function xyz() {};
function xyz() {}
> obj
xyz {}

As you can see, the console is really just looking at the constructor property of an object (which is usually inherited) and then prints the function name of the function assigned to it.

Foo.prototype.constructor is just a normal property which is assigned when a function is created. It's purpose is that actual instances of Foo (created with new Foo) have a constructor property which points to Foo. Since instances inherit all properties from the prototype.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I don't understand : what is the `Foo.prototype` type ? – Royi Namir Nov 02 '13 at 18:24
  • which is type is Foo instance. `Foo{}`....right ? (that's what the cosnsole shows) – Royi Namir Nov 02 '13 at 18:24
  • Yes, but only because an instance inherits the `constructor` property from the prototype. Chrome's `console.log` looks at the `constructor` property of an object and then assumes that the object is an instance of that object. This works in almost all cases, but not for prototype objects, because they have explicitly set a `constructor` property. – Felix Kling Nov 02 '13 at 18:25
  • ok so if the type is `Foo` and `__proto__` is ctor's prototype - why `Foo.prototype.__proto__` doesnt refer to Foo.prototype ? – Royi Namir Nov 02 '13 at 18:26
  • `g.__proto__` refers to `Foo.prototype`, which is a "plain" object. And the prototype of a plain object is `Object.prototype`. – Felix Kling Nov 02 '13 at 18:28
  • sory . i meant `Foo.prototype.__proto__` . `Foo.prototype` is instance of `Foo{}` so its __proto__ should referr to its ctor prototype... – Royi Namir Nov 02 '13 at 18:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40434/discussion-between-royi-namir-and-felix-kling) – Royi Namir Nov 02 '13 at 18:28
  • Awww... But it was interesting :( – aychedee Nov 02 '13 at 18:42
  • @aychedee you can see the chat content you know ...:-) – Royi Namir Nov 03 '13 at 18:41
  • Oh, so you can! Excellent. – aychedee Nov 04 '13 at 10:04