dotslash dotslash - 1 month ago 7
Javascript Question

Objects returned from constructor do not have a prototype

I came across this passage in the wonderful book Eloquent JavaScript:


Constructors (in fact, all functions) automatically get a property
named prototype, which by default holds a plain, empty object that
derives from Object.prototype. Every instance created with this
constructor will have this object as its prototype.


So I thought I'll test it.

function Person () {}

person = new Person()

console.log(Person.prototype) // Object {}
console.log(person.prototype) // undefined ???? :O


The first
console.log
makes sense, but why does the second one show
undefined
?

Answer

Use Object.getPrototypeOf to get the prototype of specified object

function Person() {}
person = new Person();
console.log(Person.prototype);
console.log(Object.getPrototypeOf(person));

Comments