๐คฒ ๋ ์์ปฌ ์ค์ฝํ
์๋ฐ์คํฌ๋ฆฝํธ ์์ง๋ ํจ์๋ฅผ ์ด๋์ ํธ์ถํ๋์ง๊ฐ ์๋๋ผ ํจ์๋ฅผ ์ด๋์ ์ ์ํ๋์ง์ ๋ฐ๋ผ์ ์์์ค์ฝํ๊ฐ ์ ํด์ง๋ค.
์ด๊ฒ์ ๋ ์์ปฌ ์ค์ฝํ๋ผ๊ณ ํ๋ค
=> ์ด๊ฒ์ ๋ง๋ง ํ๋ฉด ์ดํด๊ฐ ํ ์ ์๋ค. ์์ ๋ฅผ ํตํด์ ์์๋ณด์
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์์ ํญ์ ๋์ค๋ ๊ฐ๋ ์ด๋ฏ๋ก ํญ์ ๊ธฐ์ตํ๋ ค๊ณ ํ์.
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
navigator.mediaSession (0) | 2023.07.27 |
---|---|
๐ฆ๋น๋์ค ์๋์ฌ์ ์คํ์ํค๊ธฐ (0) | 2023.07.02 |
Hls.js ์ฌ์ฉ (0) | 2023.06.14 |
๐ธ ๋ธ๋ผ์ฐ์ ๋ ๋๋ง ๊ณผ์ (0) | 2023.04.12 |
๊ฐ์์(Observer) ํจํด (0) | 2022.01.23 |
๋๊ธ