Section 11: Behind the Scenes: Classes & OOP - Prototypes & More
The goals
๐ช๐ปWhat happens behind the scenes of classes and objects?
โ๐ปConstructor functions (without classes)
๐๐ปPrototype & Prototypical Inheritance
class Person {
name = 'Max';
constructor() {
this.age = 30;
}
greet() {
console.log(
"Hi, I am " + this.name + "and I am " + this.age + "years old."
);
}
}
const person = new Person();
person.greet();
function Person() {
this.age = 30;
this.name = 'Max';
this.greet = function() {
console.log(
"Hi, I am " + this.name + "and I am " + this.age + "years old."
);
}
}
const person = new Person();
person.greet();
These codes are the same actually.
What's 'new' 's job here?
In the codes above, if there is no 'new' keyword in front of Person() function it will generate errors.
Because in the function, it doesn't return any value there, but if we use 'new', it defines 'this = {}' inside of the object so it's like making an empty object and allocate the value we set. And do 'return this' at last.
This is the reason we can get the value when we use 'new' keyword.
ํ๋กํ ํ์ ์ ํท๊ฐ๋ฆฌ๊ณ ์ด๋ ค์ด ์ฃผ์ ์ผ ์ ์์ต๋๋ค - ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ํ๋กํ ํ์ ์ ์ ๋๋ก ์ดํดํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
ํ๋กํ ํ์ ์ ๋ค๋ฅธ ๊ฐ์ฒด("O"๋ผ๊ณ ํ๊ฒ ์ต๋๋ค)์ ์ฐ๊ฒฐ๋ ๊ฐ์ฒด("P"๋ผ๊ณ ํ๊ฒ ์ต๋๋ค)์ ๋๋ค - ํ๋กํ ํ์ ๊ฐ์ฒด๋ ๋ค๋ฅธ ๊ฐ์ฒด("O")์์ ์ ์๋์ง ์์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ก ์์ ํ ๋ ๋ค๋ฅธ ๊ฐ์ฒด("O")๊ฐ ์ ๊ทผํ ์ ์๋ "๋์ฒด ๊ฐ์ฒด" ๊ฐ์ ์ญํ ์ ํฉ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก JavaScript์ ๋ชจ๋ ๊ฐ์ฒด์๋ ์ด๋ฌํ ๋์ฒด ๊ฐ์ฒด๊ฐ ์์ต๋๋ค (์ฆ ํ๋กํ ํ์ ๊ฐ์ฒด)๊ฐ ์์ต๋๋ค - ์ด์ ๋ํด์๋ ์ดํ ๊ฐ์์์ ์์ธํ ์ค๋ช ํฉ๋๋ค.
์์ฑ์ ํจ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก "์์ฑ๋ " ๊ฐ์ฒด์ ๋ํ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ณผ ๋ ํนํ ํท๊ฐ๋ฆด ์ ์์ต๋๋ค. (์์ฑ์ ํจ์ ๊ฐ์ฒด์ .prototype ํ๋กํผํฐ๋ฅผ ํตํด ์ํ๋จ)
์๋์ ์์๋ฅผ ์ดํด๋ณด์ธ์
function User() {
... // ์ด๋ค ๋
ผ๋ฆฌ์ธ์ง๋ ์ค์ํ์ง ์์ต๋๋ค => user ๊ฐ์ฒด๊ฐ ๊ฐ์ง ํ๋กํผํฐ ๋ฑ์ ๊ตฌ์ฑํฉ๋๋ค
User.prototype = { age: 30 }; // user ํจ์ ๊ฐ์ฒด๊ฐ ์๋ "์์ฑ๋ " user ๊ฐ์ฒด์ ๋ํ ํ๋กํ ํ์
๊ฐ์ฒด๋ฅผ ์ค์ ํฉ๋๋ค
์ฌ๊ธฐ์ User ํจ์์๋ ๋ฌผ๋ก ํ๋กํ ํ์ ๊ฐ์ฒด(์ฆ ์ฐ๊ฒฐ๋ ๋์ฒด ๊ฐ์ฒด)๊ฐ ์์ต๋๋ค - ํ์ง๋ง ์ด๊ฒ์ prototype ํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๊ฐ ์๋๋๋ค. ๋์ ๋ชจ๋ ๊ฐ์ฒด(ํจ์๋ ๊ฐ์ฒด์)๊ฐ ๊ฐ์ง๊ณ ์๋ ํน์ __proto__ ํ๋กํผํฐ๋ฅผ ํตํด ์ฐ๊ฒฐ๋ ๋์ฒด/ํ๋กํ ํ์ ๊ฐ์ฒด์ ์ ๊ทผํ ์ ์์ต๋๋ค.
prototype ํ๋กํผํฐ๋ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ํํฉ๋๋ค. ์ด User ์์ฑ์ ํจ์๋ก ์์ฑ๋ ์ ๊ฐ์ฒด๊ฐ ๊ฐ์ง ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ์ค์ ํฉ๋๋ค.
That means:
const userA = new User();
userA.__proto__ === User.prototype; // ์ฐธ
userA.__proto__ === User.__proto__ // ๊ฑฐ์ง
Quiz
“์์ฑ์ ํจ์”๋ผ๋ ๊ฒ ๋ฌด์์ผ๊น์?
-
ํด๋์ค๋ ๋์ผํ๋ ๊ทธ์ ๋ ์ค๋๋ ๊ตฌ๋ฌธ์ ๋๋ค.
-
new ํค์๋๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ์ฌ์ฉํ์ง ์๊ณ ํธ์ถํ์ฌ ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ ํจ์์ ๋๋ค.
-
new ํค์๋์ ํจ๊ป ํธ์ถํ์ฌ ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ ํจ์์ ๋๋ค.
๋ค์์ ์ ํจํ ์์ฑ์ ํจ์์ผ๊น์?
function adminUser(usrName) {
this.name = usrName;
}
-
์๋์ค, ์์ฑ์ ํจ์๋ ๋๋ฌธ์๋ก ์์ํด์ผ ํฉ๋๋ค(์: AdminUser๊ฐ ๋ง์).
-
์๋์, ํ๋กํผํฐ ์ด๋ฆ์ ๋งค๊ฐ๋ณ์ ์ด๋ฆ๊ณผ ๊ฐ์์ผ ํฉ๋๋ค(์: this.usrName).
-
์
์ด๋ฆ์ ๋๋ฌธ์๋ก ์์ํ๋ ๊ฒ์ด ์ ๋์ ์ผ๋ก ๊ถ์ฅ๋์ง๋ง ์๋ฐํ ๋งํ์๋ฉด ํ์๋ ์๋๋๋ค.
"ํ๋กํ ํ์ ์์"์ ๊ฐ๋ ์ ๋ฌด์์ผ๊น์?
-
๊ธฐ๋ฅ์ ๊ฐ์ฒด ๊ฐ์ ๊ณต์ ๋ ์ ์์ต๋๋ค(๊ฐ์ฒด๋ ํ๋กํ ํ์ ๋ฉ์๋ ๋ฐ ํ๋กํผํฐ๋ก '๋์ฒด'๋ ์ ์์).
-
๋ฉ์๋์ ํ๋กํผํฐ๋ ํ๋์ ๊ฐ์ฒด๋ก ์ ํ๋ ์ ์์ต๋๋ค.
-
๊ฐ์ฒด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ค๋ฅธ ์์น์์ ์ฌ์ฉ๋ ์ ์๊ณ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ํ์ผ๋ก ์ ํ๋์ง ์์ต๋๋ค.
ํ๋กํ ํ์ ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐ๊ฒฐํ ์ ์๋ "๋์ฒด ๊ฐ์ฒด"์ ๋๋ค.
someObj.__proto__์ SomeFunction.prototype์ ์ฐจ์ด์ ์ ๋ฌด์์ผ๊น์?
-
์ฐจ์ด๋ ์์ต๋๋ค. ๋ ๋ค ์ ๊ทผ๋๋ ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ ๊ฐ์ ธ์ค๊ณ ์ค์ ํ๋ ๋ฐ์ ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
-
__proto__๋ someObj์ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ณ prototype์ SomeFunction์ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ผ๋ก ํ ๋น๋ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
-
__proto__๋ SomeFunction์ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค. ์๋ํ๋ฉด SomeFunction.prototype์ด (ํจ์ ์์ฒด์ ๊ทธ ํจ์์ ๊ธฐ๋ฐํ ๋ชจ๋ ๊ฐ์ฒด์ ๋ํด) ์ด ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ์ค์ ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
It's really important to understand the difference between the prototype property on constructor functions(์์ฑ์ ํจ์) where we configure what will be added to objects that are created based on that constructor function and the __proto property which is available on every object, not just on constructor functions or function objects which in the end points to the prototype that has been assigned to this object, so to the fallback object that has been assigned to that object, that is really important to understand