๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Javascript

[JS] ํ”„๋กœํ† ํƒ€์ž…(Prototype) ๋‹ค๋ค„๋ณด๊ธฐ

by Fomagran ๐Ÿ’ป 2022. 2. 12.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋Š˜๋„ ์—ญ์‹œ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด ๋ฐฐ์šด ํ”„๋กœํ† ํƒ€์ž…์„ ๋ณต์Šตํ•˜๋Š” ์ฐจ์›์—์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Prototype์ด๋ž€?

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ˆจ๊น€ ํ”„๋กœํผํ‹ฐ๋กœ Prototype์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

์ด๊ฒƒ์€ null์ด๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๊ฒƒ์„ ํ†ตํ•ด์„œ ํ•ด๋‹น ๊ฐ์ฒด์˜ ๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ)์„ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ฐธ์กฐํ•œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ˜„์žฌ ๊ฐ์ฒด์˜ 'ํ”„๋กœํ† ํƒ€์ž…'์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.


Prototype ์„ค์ •ํ•˜๊ธฐ

 

1. __proto__

 

์šด๋™์„ ์ˆ˜๋ฅผ ์ง€์นญํ•˜๋Š” 'player'๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

 

let player = {
    firstName:"Young",
    lastName:"An",
    get fullName() {
        return this.firstName + " " + this.lastName
    },
    set fullName(value) {
        [this.firstName,this.lastName] = value.split(" ")
    },
    jump: function() {
        print("jump")
    },
    run:function() {
        print("run")
    }
}

 

๋†๊ตฌ์„ ์ˆ˜ 'basketBallPlayer' ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด 'player'๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์ฐธ์กฐํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด 'player'์— ์žˆ๋Š” jump() ๋ฉ”์„œ๋“œ๋ฅผ basketBallPlayer๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

let basketBallPlayer ={
    shoot:function() {
        print("shoot")
    },
    //ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์€ __proto__ ๋ผ๊ณ  ํ”„๋กœํผํ‹ฐ๋ช…์„ ํ•ด์ค˜์•ผ ํ•จ.
    __proto__:player
}

//ํ”„๋กœํ† ํƒ€์ž…์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
basketballPlayer.jump() //jump

 

2. Object.create(obj)

 

__proto__๋ฅผ ์ง์ ‘ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋” ๋ชจ๋˜ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ”„๋กœํ† ํƒ€์ž…์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•ด๋„ __proto__์—์„œ ์„ค์ •ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ฑ„ํƒํ•œ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

let baseBallPlayer = Object.create(player)

๋‚ด์žฅ ๊ฐ์ฒด ํ”„๋กœํ† ํƒ€์ž… ์ˆ˜์ •

 

๋ฌผ๋ก  ๋‚ด์žฅ ๊ฐ์ฒด ํ”„๋กœํ† ํƒ€์ž…์— ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด String์˜ ํ”„๋กœํ† ํƒ€์ž…์— sayHi() ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

String.prototype.sayHi = function() {
    print("hi")
}

 

ํ•˜์ง€๋งŒ ์ด์™€ ๊ฐ™์ด ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ๋ฎ์–ด์“ธ ์œ„ํ—˜์ด ์žˆ์œผ๋ฏ€๋กœ ์“ฐ์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

๋‹จ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ช…์„ธ์„œ์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ(ํ’€๋ฆฌํ•„)์€ ํ—ˆ์šฉ๋œ๋‹ค.

 

๋˜ํ•œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด์žฅ ๊ฐ์ฒด๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์ฑ„ํƒํ•  ์ˆ˜๋„ ์žˆ๋‹ค.


์ƒ์†๋ฐ›์€ ๋ฉ”์„œ๋“œ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

 

hasOwnProperty(ํ”„๋กœํผํ‹ฐ๋ช…) ์œผ๋กœ ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์†๋ฐ›์•˜๋Š”์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

for (let key in basketballPlayer) {
    //์ƒ์†๋ฐ›์€ ๋ฉ”์„œ๋“œ์ธ์ง€ ์•„๋Š” ๋ฒ•
    if (basketballPlayer.hasOwnProperty(key)) {
        print("๋‚ด ํ”„๋กœํผํ‹ฐ")
    }else {
        print("์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ")
    }
    print(key)
}

/*
๋‚ด ํ”„๋กœํผํ‹ฐ
shoot
์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ
jump
์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ
firstName
์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ
lastName
์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ
fullName
์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ
run
*/

Reference

 

 

๋ชจ๋˜ JavaScript ํŠœํ† ๋ฆฌ์–ผ

 

ko.javascript.info

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€