Relationships between Constructor, Prototype, and Instance

·

1 min read

I think it is quite tricky to find out the relationships between them.

So, I made a picture to understand the relationships easily.

생성자 함수 및 프로토타입 관계.png In the above picture, "Trispace" font provided by "The Trispace Project Authors" is used.

From now on, let me show you the codes to prove this.

I used Chrome to print the results.

Settings

The codes below show what the constructor, the prototype, and the instance are.

// settings
function Person(name) {
  this.name = name;
}

Person.prototype.printName = function () {
  return this.name;
};

const person1 = new Person('Bada');

// info of the instance
console.log(person1); // { name: "Bada" }
console.log(person1.printName()); // Bada

About the Constructor

console.dir(Person); // ƒ Person(name)
console.dir(Person.__proto__); // ƒ anonymous()
console.dir(Person.prototype);
// { printName: ƒ (), constructor: ƒ Person(name) }

About 'f anonymous'

In [About the Constructor], Person.__proto__ was f anonymous. So, I wanted to know __proto__ of it.

console.dir(Person.__proto__.__proto__);
// Object [constructor, hasOwnProperty, isPrototypeOf, ...]

About the Instance

console.log(person1); // { name: "Bada" }
console.dir(person1.__proto__);
// { printName: ƒ (), constructor: ƒ Person(name) }
console.dir(person1.prototype); // undefined
console.dir(person1.constructor); // ƒ Person(name)

About the Prototype

console.dir(Person.prototype);
// { printName: ƒ (), constructor: ƒ Person(name) }
console.dir(Person.prototype.constructor); // ƒ Person(name)
console.dir(Person.prototype.__proto__);
// Object [constructor, hasOwnProperty, isPrototypeOf, ...]