Encapsulate Variable, ๋ณ€์ˆ˜ ์บก์Šํ™”ํ•˜๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

let defaultOwner = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
let defaultOwnerData = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
export function defaultOwner() {
    return defaultOwnerData;
}
 
export function setDefaultOwner(arg) {
    defaultOwnerData = arg;
}

๋ฐฐ๊ฒฝ

  • ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ณด๋‹ค ๋‹ค๋ฃจ๊ธฐ๊ฐ€ ์ˆ˜์›”ํ•˜๋‹ค.
  • ๋ฐ˜๋ฉด์— ๋ฐ์ดํ„ฐ๋Š” ๋‹ค๋ฃจ๊ธฐ๊ฐ€ ๊นŒ๋‹ค๋กญ๋‹ค. ์œ ํšจ๋ฒ”์œ„๊ฐ€ ๋„“์–ด ๊ณ ์ณ์•ผํ•  ๊ณณ์ด ๋งŽ์•„์ง„๋‹ค.
  • ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๊ฐ€ ๋„“์€ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฎ๊ธธ ๋•Œ๋Š”, ๊ทธ ๋ฐ์ดํ„ฐ๋กœ์˜ ์ ‘๊ทผ์„ ๋…์ ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  • ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์•ผ๋งŒ ๋ณ€๊ฒฝํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋ณ€๊ฒฝ ์ „ ๊ฒ€์ฆ์ด๋‚˜ ์ถ”๊ฐ€ ๋กœ์ง์„ ์‰ฝ๊ฒŒ ๋ผ์›Œ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ์ฒด ์ง€ํ–ฅ์—์„œ ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ญ์ƒ private ์œผ๋กœ ์œ ์ง€ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ•์กฐํ•˜๋Š” ์ด์œ ๊ฐ€ ์—ฌ๊ธฐ์— ์žˆ๋‹ค.
  • ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•ด์•ผ ํ•œ๋‹ค.
  • ์ด๋ณด๋‹ค ์ข‹์€ ๊ฒƒ์€ ๋ถˆ๋ณ€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ ˆ์ฐจ

  1. ๋ณ€์ˆ˜๋กœ์˜ ์ ‘๊ทผ๊ณผ ๊ฐฑ์‹ ์„ ์ „๋‹ดํ•˜๋Š” ์บก์Šํ™” ํ•จ์ˆ˜๋“ค์„ ๋งŒ๋“ ๋‹ค.
  2. ์ •์  ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  3. ๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜๋˜ ๋ถ€๋ถ„์„ ๋ชจ๋‘ ์ ์ ˆํ•œ ์บก์Šํ™” ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ๋ฐ”๊พผ๋‹ค. ํ•˜๋‚˜์”ฉ ๋ฐ”๊ฟ€ ๋•Œ๋งˆ๋‹ค ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  4. ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•œ๋‹ค.
    • ๋ณ€์ˆ˜๋กœ์˜ ์ง์ ‘ ์ ‘๊ทผ์„ ๋ง‰์„ ์ˆ˜ ์—†์„ ๋•Œ๋„ ์žˆ๋‹ค. (์ปดํŒŒ์ผ๋Ÿฌ ์˜ค๋ฅ˜?)
    • ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ด๋ฆ„์ž์ฒด๋ฅผ ๋ฐ”๊ฟ”๋ฒ„๋ฆฌ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋œจ๋‹ˆ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  5. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  6. ๋ณ€์ˆ˜ ๊ฐ’์ด ๋ ˆ์ฝ”๋“œ๋ผ๋ฉด ๋ ˆ์ฝ”๋“œ ์บก์Šํ™”ํ•˜๊ธฐ๋ฅผ ์ ์šฉํ• ์ง€ ๊ณ ๋ คํ•œ๋‹ค.

๋”ฐ๋ผํ•˜๊ธฐ

let defaultOwner = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
spaceship.owner = defaultOwner;
 
defaultOwner = {firstName: "๋ ˆ๋ฒ ์นด", lastName: "ํŒŒ์Šจ์Šค"};
  • ์ „์—ญ ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๊ฒฝ์šฐ๋‹ค.

  • ์ฐธ์กฐํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค.

  • ์ด๋ฒˆ์—๋Š” ๊ฐฑ์‹ ํ•˜๋Š” ์ฝ”๋“œ๋„ ์žˆ๋‹ค.

1. ์บก์Šํ™”๋ฅผ ์œ„ํ•ด ์ฝ๊ณ  ์“ฐ๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์ž.

let defaultOwner = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
function defaultOwner() {
    return defaultOwner;
}
 
function setDefaultOwner(arg) {
    defaultOwner = arg;
}
 
spaceship.owner = defaultOwner;
 
defaultOwner = {firstName: "๋ ˆ๋ฒ ์นด", lastName: "ํŒŒ์Šจ์Šค"};

2. ์ฐธ์กฐํ•˜๋Š” ๊ณณ์„ ์ฐพ์•„ ๋ฐ”๊พผ๋‹ค.

let defaultOwner = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
function defaultOwner() {
    return defaultOwner;
}
 
function setDefaultOwner(arg) {
    defaultOwner = arg;
}
 
spaceship.owner = defaultOwner();
 
setDefaultOwner({firstName: "๋ ˆ๋ฒ ์นด", lastName: "ํŒŒ์Šจ์Šค"});
  • ํ•˜๋‚˜์”ฉ ๋ฐ”๊ฟ€๋•Œ๋งˆ๋‹ค ํ…Œ์ŠคํŠธํ•˜์ž.

3. ๋ชจ๋“  ์ฐธ์กฐ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค๋ฉด ๋ณ€์ˆ˜์˜ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•˜์ž.

// defaultOwner.js
let defaultOwner = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
export function defaultOwner() {
    return defaultOwner;
}
 
export function setDefaultOwner(arg) {
    defaultOwner = arg;
}

๊ฐ’ ์บก์Šํ™”ํ•˜๊ธฐ

  • ์œ„์˜ ๋ฐฉ๋ฒ•๋Œ€๋กœ ํ•˜๋ฉด ๋ณ€์ˆ˜ ์ž์ฒด์— ์ ‘๊ทผํ•ด์„œ ๊ฐ’์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ํ•˜์ง€๋งŒ ๋ ˆํผ๋Ÿฐ์Šค ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ–‰์œ„๋Š” ์ œ์–ดํ•  ์ˆ˜ ์—†๋‹ค.
const owner1 = defaultOwner();
assert.equal("ํŒŒ์šธ๋Ÿฌ", owner1.lastName, "์ฒ˜์Œ ๊ฐ’ ํ™•์ธ");
const owner2 = defaultOwner();
owner2.lastName = "ํŒŒ์Šจ์Šค";
assert.equal("ํŒŒ์Šจ์Šค", owner1.lastName, "owner2๋ฅผ ๋ณ€๊ฒฝํ•œ ํ›„");
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆํผ๋Ÿฐ์Šค ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋Š” ํ•ญ์ƒ ๋ณต์ œ๋ณธ์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜์ž.
// defaultOwner.js
let defaultOwnerData = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
export function defaultOwner() {
    return Object.assign({}, defaultOwnerData);
}
 
export function setDefaultOwner(arg) {
    defaultOwnerData = arg;
}
  • ํŠนํžˆ ๋ฆฌ์ŠคํŠธ์— ์ด ๊ธฐ๋ฒ•์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์›๋ณธ์„ ๋ณ€๊ฒฝํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?
  • ์ฆ‰, defaultOwnerData ์˜ ๋‚ด๋ถ€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ๋‹ค. (firstName)
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ ˆ์ฝ”๋“œ ์บก์Šํ™”ํ•˜๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์ž.
// defaultOwner.js
let defaultOwnerData = {firstName: "๋งˆํ‹ด", lastName: "ํŒŒ์šธ๋Ÿฌ"};
 
export function defaultOwner() {
    return new Person(defaultOwnerData);
}
 
export function setDefaultOwner(arg) {
    defaultOwnerData = arg;
}
 
class Person {
    constructor(data) {
        this._lastName = data.lastName;
        this._firstName = data.firstName;
    }
 
    get lastName() {
        return this._lastName;
    }
 
    get firstName() {
        return this._firstName;
    }
}
  • ์ง€๊ธˆ๊นŒ์ง€ ๊ฒŒํ„ฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜๋‹ค.
  • ๊ทธ๋Ÿฐ๋ฐ ์„ธํ„ฐ์—์„œ๋„ ๋ณต์ œ๋ณธ์„ ๋งŒ๋“ ๋Š ๊ฒƒ์ด ์ข‹๋‹ค.
  • ๋ณต์ œ๊ฐ€ ์ฃผ๋Š” ์„ฑ๋Šฅ์ด ๊ฑฑ์ •๋  ์ˆ˜ ์žˆ์œผ๋‚˜, ์˜ํ–ฅ์€ ๋ฏธ๋ฏธํ•˜๋‹ค.
  • ์˜คํžˆ๋ ค ์›๋ณธ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์— ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ๊ฐ€ ๋” ๋งŽ๋‹ค.
  • ๋‹ค๋งŒ ์ง€๊ธˆ ์—์‹œ์—์„œ๋Š” ๋ณต์ œ๋ณธ์„ ๋งŒ๋“ค ๋–„ depth๊ฐ€ 1์ธ ๊ฒฝ์šฐ๋งŒ ํŒŒ์•…ํ–ˆ๋‹ค. ๋” ๊นŠ์„ ๊ฒฝ์šฐ ๋ณต์žกํ•ด์ง„๋‹ค.

Reference