๐Ÿ•๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŠœํ† ๋ฆฌ์–ผ ํ”„๋กœํ† ํƒ€์ž… ๊ณต๋ถ€ ์š”์  ์ •๋ฆฌ

leehyunjuยท2022๋…„ 7์›” 16์ผ
0
post-thumbnail
post-custom-banner

JS ํ”„๋กœํ† ํƒ€์ž…์ด๋ž€ ?

๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ์ค‘์— getter์™€ setter๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ตฌ๊ฐ„์ด ์žˆ๋‹ค.


let user = { name: 'j', get fullName() { return `${this.name}`,
set fullName(value){[this.name]=value}}}

๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ setter ๋กœ ์„ค์ •ํ•˜๊ณ  getter๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž…์ด๋ž€?

  • ๋ถ€๋ชจ์— ์ •์˜๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ์ž์‹์ด ๊ฐ€์ ธ๋‹ค ์“ด๋‹ค.

let animal = {eats:true;}
let rabbit = {jumps: true};
rabbit.__proto__ = animal;

๊ฐœ๋…

๋ถ€๋ชจ์˜ ์†์„ฑ๋„ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ํ”„๋กœํ† ํƒ€์ž…์˜ ์˜๋ฏธ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ ธ๋‹ค ์“ธ ๋•Œ ํ”„๋กœํ† ํƒ€์ž…์„ ์„ ์–ธํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

let animal = {eats: true};
let rabbit = {jumps:true, __proto__:animal};

rabbit์ด jumps๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ณ , ํ”„๋กœํ† ํƒ€์ž…(์†์„ฑ)์€ animal์„ ์ƒ์†์‹œ์ผœ์ค€๋‹ค.

let small_rabbit = {legnth: 10, __proto__: rabbit};

small_rabbit.eats ๋Š” true ๊ฐ€ ๋˜๊ณ 
small_rabblit์„ ์ถœ๋ ฅํ•˜๋ฉด {legnth: 10} ์ด ์ถœ๋ ฅ ๋œ๋‹ค.
small_rabbit.jumps๋ฅผ ํ•ด๋„ true๊ฐ€ ๋‚˜์˜จ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž…์ด๋ผ๋Š” ๊ฑธ๋กœ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ๋จผ์ €

let animal = {eats: true} ๋ผ๊ณ  ํ•  ๋•Œ
function Rabbit(name) { this.name = name; }
Rabbit.prototype = animal; ์ถœ๋ ฅ์‹œ

๊ฒฐ๊ณผ { eats : true }

let rabbit = new Rabbit("white_rabbit");
result = undefined

rabbit
result = Rabbit {name: 'white_rabbit'}

let arr = [1,2,3];

ํ”„๋กœํ† ํƒ€์ž…์˜ ๊ธฐ๋ณธ์€ Array์ด๋‹ค.

profile
์•„๋Š‘ํ•œ ๋‡Œ๊ณต๊ฐ„ ๐Ÿง 
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€