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 :

asked Nov 2, 2013 at 18:19

1 Answer 1

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
149k145 gold badges504 silver badges833 bronze badges
answered Nov 2, 2013 at 18:22
Sign up to request clarification or add additional context in comments.

10 Comments

I don't understand : what is the Foo.prototype type ?
which is type is Foo instance. Foo{}....right ? (that's what the cosnsole shows)
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.
ok so if the type is Foo and __proto__ is ctor's prototype - why Foo.prototype.__proto__ doesnt refer to Foo.prototype ?
g.__proto__ refers to Foo.prototype, which is a "plain" object. And the prototype of a plain object is Object.prototype.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.