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

[JS] Static,Protected,Private์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ

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

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

 

์˜ค๋Š˜๋„ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฐฐ์šด Static,Protected,Private ์— ๋Œ€ํ•ด ์ œ๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ

 

์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

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


Static

 

Static์€ ์ •์ ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด, ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค.

 

์ด๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ์ •์  ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํด๋ž˜์Šค์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด๋‚˜ ๊ธฐ๋Šฅ์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด ๋†“๋Š” ๊ฒƒ์ด๋‹ค.

 

Static Property 

 

class Person {
    //๋ชจ๋“  ์‚ฌ๋žŒ์ด ๋™์ผํ•˜๊ฒŒ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ’
    static planet = "์ง€๊ตฌ"
}
//ํด๋ž˜์Šค ์ž์ฒด์—์„œ ๋ฐ”๋กœ ์ฝ๊ธฐ ๊ฐ€๋Šฅ
console.log(Person.planet) //์ง€๊ตฌ

 

Static Method

 

class User {
    constructor(name,createDate) {
        this.name = name
        this.createDate = createDate
    }
    //๋ชจ๋“  ์œ ์ €๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ
    static sayHi() {
        console.log("Hi")
    }
    static compare(user1,user2) {
        return user1.createDate - user2.createDate
    }
}
//ํด๋ž˜์Šค ์ž์ฒด์—์„œ ํ˜ธ์ถœ๊ฐ€๋Šฅ
User.sayHi() //Hi

let users = [
    new User("Foma",new Date(2021,1,1)),
    new User("Gran",new Date(2020,1,1)),
    new User("Young",new Date(2022,1,1))
]
//์•„๋ž˜์™€ ๊ฐ™์ด ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Œ
console.log(users.sort(User.compare))
/*
[
  User { name: 'Gran', createDate: 2020-02-01T06:00:00.000Z },
  User { name: 'Foma', createDate: 2021-02-01T06:00:00.000Z },
  User { name: 'Young', createDate: 2022-02-01T06:00:00.000Z }
]
*/

//์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๋„ ์œ ์šฉํ•จ
User.createTodayNewUser = function() {
    return new this("Foma",new Date())
}

Protected

 

Protected๋Š” ํด๋ž˜์Šค์™€ ํด๋ž˜์Šค์˜ ์ž์‹๊นŒ์ง€ ํ—ˆ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

 

์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹์€ ์ด๋ฆ„ ์•ž์— '_'๋ฅผ ๋ถ™์ด๊ณ  ์ž‘์„ฑํ•œ๋‹ค.

 

_waterAmount = 0
_plusWaterAmount1() {...}

 

ํ•˜์ง€๋งŒ JavaScript ๋ฌธ๋ฒ•์ ์œผ๋กœ ์ง€์ •ํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋‘ ํ˜ธ์ถœ์ด ๋œ๋‹ค.

 

๊ฐ•์ œํ•œ ์‚ฌํ•ญ์ด ์•„๋‹ˆ๋ฉฐ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค ์‚ฌ์ด์—์„œ ์•ฝ์†์œผ๋กœ '_'์ด ์žˆ๋‹ค๋ฉด ์™ธ๋ถ€ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋‹ค๊ณ  ์—ฌ๊ธด๋‹ค.

 

console.log(a._waterAmount) //0
a._plusWaterAmount1() //waterAmount + 1
console.log(a._waterAmount) //1

Private

 

Private๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ํ—ˆ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

 

์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹์€ ์ด๋ฆ„ ์•ž์— '#'์„ ๋ถ™์ด๊ณ  ์ž‘์„ฑํ•œ๋‹ค.

 

#waterAmount = 0
#plusWaterAmount1() { ... }

 

Protected์™€๋Š” ๋‹ค๋ฅด๊ฒŒ Private๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ ๊ฐ•์ œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด Error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

a.#plusWaterAmount() //Error
console.log(a.#waterAmount) //Error

Reference

 

 

The Modern JavaScript Tutorial

We want to make this open-source project available for people all around the world. Help to translate the content of this tutorial to your language!

javascript.info

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€