October 31, 2020
์ด์ ๊ธ ์ฐธ์กฐ.
__proto__ ๋ฅผ ๋์ฒดํ๋ ๋ฐฉ์์ด๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์๊ฒ ํผ์นด์ธ๋ฅผ ๋ถ๋ชจ๋ก ํ๋ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ผ ๋ช ๋ นํ ๋ ์ด๋ค.
let pikachu = {
attack: 90,
defence: 50,
skillOne: 'toSlapYourFace',
skillTwo: 'lightning shock',
}
let raichu = Object.create(pikachu)
raichu
// {} ๋น์ด์๋ค. ํ์ง๋ง __proto__ ๊ฐ ์ถ๋ํ๋ค๋ฉด ์ด๋จ๊น?
__proto__: attack: 90
defence: 50
skillOne: 'toSlapYourFace'
skillTwo: 'lightning shock'
__proto__: Object
Object.create() ์ ์ธ์์ ๋ถ๋ชจ ๊ฐ์ฒด๋ฅผ ํ์์ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋๋๋ฐ, ๊ทธ ์๋ก์ด ๊ฐ์ฒด๋ pikachu ๋ฅผ ๋ถ๋ชจ๋ก ํ๋ ์๋ก์ด ๊ฐ์ฒด ์ด๋ค.
๊ทธ๋์ raichu ๊ฐ ๋ง๋ค์ด ์ก๋๋ฐ ์๋ฌด๊ฒ๋ ์๋ ํ ํ ๋น ๊ฐ์ฒด ๊ฐ๋ค.
ํ์ง๋ง raichu.__proto__ ๋ ์์์ ๋ณด๋ฏ์ด ์ ํํ pikachu ๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ค.
raichu.cuteness = 100
100
raichu.specialSkill = 'jungwangsukhwa'
;('jungwangsukhwa')
raichu.canEvolve = true
true
์ด์ ๋ผ์ด์ธ๋ ๊ท์ฌ์, ํน์์คํฌ, ์งํ๊ฐ๋ฅ์ฌ๋ถ ์ ์์ฑ์ ์ป์๋ค.
raichu
{cuteness: 100, specialSkill: "jungwangsukhwa", canEvolve: true}
canEvolve: true
cuteness: 100
specialSkill: "jungwangsukhwa"
__proto__:
attack: 90
defence: 50
skillOne: "toSlapYourFace"
skillTwo: "lightning shock"
__proto__: Object
๋ผ์ด์ธ๋ ํผ์นด์ธ๋ฅผ ๋ถ๋ชจ๋ก ํ๋ ์๋ก์ด ๊ฐ์ฒด๊ฐ ๋๋ค.
์ ๊ธ์ ์ด์ด ๋ง์ฐฌ๊ฐ์ง๋ก ๋ผ์ด์ธ ๊ณต๊ฒฉ๋ ฅ์ ๋ ๋ฐฐ๋ก ๋ปฅํ๊ธฐ ํ๋ค.
raichu.attack = 180
180
pikachu.attack
90 // ๋ถ๋ชจ ์์ฑ์ ๊ทธ๋๋ก ์ด๋ค.
์ด์ฒ๋ผ __proto__ ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ณด๋ค Object.create() ๋ฅผ ํตํด ๊ฐ์ฒด์ ๊ฐ์ฒด๊ฐ์ ์์๊ด๊ณ, ํ๋กํ ๋งํฌ๋ฅผ ์ง์ ํด ์ฃผ๋ ๊ฒ์ด ๋ ์ข๋ค.
๋ง์ง๋ง์ผ๋ก,
raichu.__proto__
{attack: 90, defence: 50, skillOne: "toSlapYourFace", skillTwo: "lightning shock"}
raichu.__proto__ === pikachu
true