๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript

๐Ÿ‘ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„ & ํด๋กœ์ €

by frontChoi 2023. 3. 7.
๋ฐ˜์‘ํ˜•

๐Ÿคฒ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง€๋Š” ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ํ˜ธ์ถœํ–ˆ๋Š”์ง€๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ์ •์˜ํ–ˆ๋Š”์ง€์— ๋”ฐ๋ผ์„œ ์ƒ์œ„์Šค์ฝ”ํ”„๊ฐ€ ์ •ํ•ด์ง„๋‹ค.

์ด๊ฒƒ์„ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„๋ผ๊ณ  ํ•œ๋‹ค

=> ์ด๊ฒƒ์„ ๋ง๋งŒ ํ•˜๋ฉด ์ดํ•ด๊ฐ€ ํ•  ์ˆ˜ ์—†๋‹ค. ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ์•Œ์•„๋ณด์ž

 

const x = 1;

function foo() {
  const x = 10;
  console.log(x);
  bar(); //1
}

function bar() {
  console.log(x);
}

foo(); // 10
bar(); // 1

foo ํ•จ์ˆ˜ ์•ˆ์—์„œ bar ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์˜€์ง€๋งŒ, ๊ฒฐ๊ณผ๋Š” 1์ด ๋‚˜์˜จ๋‹ค. ์™œ๋ƒํ•˜๋ฉด barํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜๋Š” ๊ณณ์ด fooํ•จ์ˆ˜ ์•ˆ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— fooํ•จ์ˆ˜์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„๋Š” ์ „์—ญํ•จ์ˆ˜์ด๊ณ  ์ „์—ญํ•จ์ˆ˜์˜ x=1์ด๋‹ค.

const x = 1;

function outerFunc() {
  const x = 10;

  function innerFunc() {
    console.log(x);
  }

  innerFunc(); //10
}

outerFunc();

innerFuncํ•จ์ˆ˜๋Š” outerFuncํ•จ์ˆ˜ ์•ˆ์—์„œ ์ •์˜๊ฐ€ ๋˜์—ˆ๊ณ , outerFuncํ•จ์ˆ˜ ์•ˆ์—์„œ innerFuncํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์˜€๋‹ค.

console.log์— ์ฐํžˆ๋Š”๊ฒƒ์€ 10์ด๋‹ค. innerFunc๊ฐ€ outerFunc ํ•จ์ˆ˜ ์•ˆ์—์„œ ์„ ์–ธ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— innerFunc์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„๋Š” outerFunc์ด๋‹ค.

 

๊ฒฐ๋ก ์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์‹œ์ ์ด ์•„๋‹Œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์‹œ์ ์— ์Šค์ฝ”ํ”„๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.

 

 

๐Ÿ‘Š  ํด๋กœ์ €

์ค‘์ฒจํ•จ์ˆ˜๊ฐ€ ์™ธ๋ถ€ํ•จ์ˆ˜๋ณด๋‹ค ๋” ์˜ค๋ž˜ ์œ ์ง€๋˜๋Š”๊ฒฝ์šฐ๋ฅผ ํด๋กœ์ €๋ผ๊ณ  ํ•œ๋‹ค.

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž

const x = 1;

function outer() {
  const x = 10;
  const inner = function () {
    console.log(x);
  };

  return inner;
}

const innerFunc = outer();
innerFunc();

์œ„์˜ ์˜ˆ์ œ๋Š” outer ํ•จ์ˆ˜์•ˆ์— inner๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์˜€๊ณ ,inner ๋ณ€์ˆ˜ ์•ˆ์—  ํ•จ์ˆ˜๋ฅผ ๋Œ€์ž…ํ•˜์˜€๊ณ , ํ•จ์ˆ˜๋ฅผ returnํ•˜์˜€๋‹ค.

๊ทธ๋ฆฌ๊ณ  innerFunc์— return๋œ ํ•จ์ˆ˜๋ฅผ ๋‹ด๊ณ  innerFunc ํ•จ์ˆ˜๋ฅผํ˜ธ์ถœํ•˜์˜€๋‹ค.

์ƒ์œ„ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ์ข…๋ฃŒ๋˜์—ˆ์Œ์—๋„  ๋ถˆ๊ตฌํ•˜๊ณ ,  ์ƒ์œ„ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ๊ฒƒ์€  ์ž์‹ํ•จ์ˆ˜๊ฐ€ ์ž์‹ ์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ๋•Œ๋ฌธ์— outer์˜ x์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

โœŒ๏ธ ํด๋กœ์ €์˜ ํ™œ์šฉ

ํด๋กœ์ €๋Š” ์ƒํƒœ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ™•์ธํ•ด๋ณด์ž,

 

let num = 0;

const increase = function () {
  return ++num;
};

console.log(increase());
console.log(increase());
console.log(increase());
  • ํ˜„์žฌ num์€ ๋ˆ„๊ตฌ๋‚˜ ๋‹ค ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋ฆฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์ด ๋–จ์–ด์งˆ์ˆ˜ ์žˆ๋‹ค.

๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ์œ„ํ•ด num์„ ์€๋‹‰ํ™” ์‹œ์ผœ๋ณด์ž

const increase = (function () {
  let num = 0;

  return function () {
    return ++num;
  };
})();

console.log(increase());
console.log(increase());
console.log(increase());

์ฆ‰์‹œ ์‹คํ–‰ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜๋Š” ์‹คํ–‰ํ•˜์ž๋งˆ์ž ์†Œ๋ฉธ๋˜๊ณ  increase์—๋Š” return๋œ ํ•จ์ˆ˜๋ฅผ ๋‹ด๋Š”๋‹ค.

์—ฌ๊ธฐ์„œ num์€ ์ƒ์œ„์Šค์ฝ”ํ”„์˜ num์ด๋ฏ€๋กœ ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜์–ด๋„ ์ƒ์œ„์Šค์ฝ”ํ”„๋ฅผ ๊ธฐ์–ตํ•˜๊ฒŒ ๋œ๋‹ค.

 

๊ทธ๋ฆฌํ•˜์—ฌ increaseํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ƒ์œ„์Šค์ฝ”ํ”„์˜ num์ด ์ฆ๊ฐ€ํ•œ๋‹ค.

 

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ increase,decrease๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž

const Counter = (function () {
  let num = 0;

  function Counter() {}

  Counter.prototype.increase = function () {
    return ++num;
  };

  Counter.prototype.decrease = function () {
    return num > 0 ? --num : 0;
  };

  return Counter;
})();

const counter = new Counter();
console.log(counter.increase());
console.log(counter.increase());

console.log(counter.decrease());
console.log(counter.decrease());

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ prototype์„ ์‚ฌ์šฉํ•˜๋ฉด Counter์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€์—†์ด increase,decrease ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, prototype.increase, prototype.decrease๋Š” ์„ ์–ธํ• ๋•Œ ์ƒ์œ„์Šค์ฝ”ํ”„๊ฐ€ ์ฆ‰์‹œ์‹คํ–‰์ˆ˜์—ฌ์„œ num์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

๐ŸŒ ์บก์Аํ™”์™€ ์ •๋ณด์€๋‹‰

์บก์Аํ™”๋Š” ๊ฐ์ฒด์— ํ”„๋กœํผํ‹ฐ(์†์„ฑ)๊ณผ ๋ฉ”์„œ๋“œ(ํ–‰๋™)์„ ํ•˜๋‚˜๋ผ ๋ฌถ์€๊ฒƒ์„ ๋งํ•˜๊ณ , ํ”„๋กœํผํ‹ฐ,๋ฉ”์†Œ๋“œ๋“ฑ์„ ์™ธ๋ถ€์— ๊ณต๊ฐœํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„๋•Œ ๊ทธ๊ฒƒ์„ ์ •๋ณด์€๋‹‰ํ™” ๋ผ๊ณ  ํ•œ๋‹ค.

๋‹ค๋งŒ javascript๋Š” public,private,protected๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

๊ธฐ๋ณธ์ ์ธ ๊ฐ์ฒด๋ฅผ ์‚ดํŽด๋ณด์ž

function Person(name, age) {
  this.name = name; //public
  let _age = age; //private

  this.sayHi = function () {
    console.log(`Hi! My name is ${this.name}. I am ${_age}`);
  };
}

const person = new Person("choi", 30);

person.sayHi();
console.log(person.name);
console.log(person._age);

Person์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  name์€ publicํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  _age๋Š” ์ง€์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•จ์œผ๋กœ์จ Person์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰ private์ด๋‹ค.

๋‹ค๋งŒ ์ด ๊ฐ์ฒด์˜ ๋‹จ์ ์€ sayHi๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋งˆ๋‹ค ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋‹ค.

 

๊ทธ๋ฆฌํ•˜์—ฌ sayHi๋ฅผ prototype์— ์ƒ์„ฑํ•˜์ž.

function Person(name, age) {
  this.name = name; //public
  let _age = age; //private
}

Person.prototype.sayHi = function () {
  console.log(`Hi! My name is ${this.name}. I am ${_age}`);
};

const me = new Person("choi", 31);
me.sayHi();
console.log(me.name);
console.log(me._age);

ํ”„๋กœํ† ํƒ€์ž…์— sayHi๋ฅผ ๋งŒ๋“ค์—ˆ์ง€๋งŒ, prototype.sayHi์—์„œ๋Š” _age์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๋‹ค. prototype.sayHi ์ƒ์œ„ ์Šค์ฝ”ํ”„๋Š” ์ „์—ญ์Šค์ฝ”ํ”„์ด๊ณ , ์ „์—ญ์Šค์ฝ”ํ”„์—๋Š” _age๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ฆฌํ•˜์—ฌ me.sayHi()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

 

 

์ด๋ฒˆ์—๋Š” ํด๋กœ์ €๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋ณด์ž

const Person = (function () {
  let _age = 0; //private

  function Person(name, age) {
    this.name = name; //private
    _age = age;
  }

  Person.prototype.sayHi = function () {
    console.log(`Hi! My name is ${this.name}. I am ${_age}`);
  };

  return Person;
})();

const me = new Person("choi", 30);
me.sayHi();

const you = new Person("kim", 40);
you.sayHi();

_age๋ฅผ ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜์—์„œ ์„ ์–ธํ•˜๊ณ , Person๊ฐ์ฒด์— age๋ฅผ ๋ฐ›์•„์„œ _age๋ฅผ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  prototype.sayHi์—์„œ๋Š” ์ƒ์œ„์Šค์ฝ”ํ”„๊ฐ€ ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜์–ด๋„ , prototype.sayHi๋Š” ์ƒ์œ„์Šค์ฝ”ํ”„๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— _age์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์ด๋ฒˆ์—๋Š” ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„์™€ ํด๋กœ์ €์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. javascript์—์„œ ํ•ญ์ƒ ๋‚˜์˜ค๋Š” ๊ฐœ๋…์ด๋ฏ€๋กœ ํ•ญ์ƒ ๊ธฐ์–ตํ•˜๋ ค๊ณ  ํ•˜์ž.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€