๐ฑ dreaming DiNO
[JS] ํด๋์ค ์์ ์ ์ฝ๋ฐฑ ํจ์ ์ ๋ฆฌ ๋ณธ๋ฌธ
class Counter {
constructor() {
this.counter = 0;
}
increase() {
this.counter++;
console.log(`counter : ${this.counter}`);
if(this.counter %5 === 0){
console.log('yo!')
}
}
}
const coolCounter = new Counter();
function printSomething() {
console.log('yo!')
}
coolCounter.increase(); // counter : 1
coolCounter.increase(); // counter : 2
coolCounter.increase(); // counter : 3
coolCounter.increase(); // counter : 4
coolCounter.increase(); // counter : 5
// yo!
// ๋ฌธ์ ์ : counter Class ์์ฒด์์ yo! ๋ณด์ฌ์ฃผ๊ธฐ ๋๋ฌธ์ control ํ ์๊ฐ ์์.
class Counter {
constructor() {
this.counter = 0;
}
increase(runIf5Times) { // ์ฝ๋ฐฑํจ์ ์ถ๊ฐ
this.counter++;
console.log(`counter : ${this.counter}`);
if(this.counter %5 === 0){
runIf5Times(this.counter) // ์ฝ๋ฐฑํจ์ ํธ์ถ // counter ์ถ๊ฐ
}
}
}
const coolCounter = new Counter();
// ์ถ๊ฐํ ์ฝ๋ฐฑํจ์ ์ ์ธ
function printSomething(num) { // ์ธ์ ์ถ๊ฐ
console.log(`yo! ${num}`)
}
// ๋ ์ถ๊ฐํ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ฝ๋ฐฑํจ์ ์ ์ธ
function alertNum(num) { // ์ธ์ ์ถ๊ฐ
alert(`wow! ${num}`)
}
// increase ๋ถ๋ฅผ๋๋ง๋ค ์ฝ๋ฐฑํจ์๋ฅผ ๋ถ๋ฅธ๋ค
coolCounter.increase(printSomething); // counter : 1
coolCounter.increase(printSomething); // counter : 2
coolCounter.increase(printSomething); // counter : 3
coolCounter.increase(printSomething); // counter : 4
coolCounter.increase(printSomething); // counter : 5
// yo! 5
coolCounter.increase(printSomething); // counter : 6
coolCounter.increase(printSomething); // counter : 7
coolCounter.increase(printSomething); // counter : 8
coolCounter.increase(printSomething); // counter : 9
coolCounter.increase(alertNum); // counter : 10
// yo! 10
// alert ์ฐฝ wow 10
// ์ฅ์ : Control ๊ฐ๋ฅํด์ง
// ๋จ์ : increase ํธ์ถ ํ ๋๋ง๋ค ์ฝ๋ฐฑํจ์๋ฅผ ์ ๋ฌํด์ ํธ์ถํด์ผ ํจ
class Counter {
constructor(runEveryFiveTimes) { // ์์ฑ์์ ์ฝ๋ฐฑํจ์ ์ถ๊ฐ
this.counter = 0;
this.callback = runEveryFiveTimes;
}
increase() { // ์ฝ๋ฐฑํจ์ ์ถ๊ฐ
this.counter++;
console.log(`counter : ${this.counter}`);
if(this.counter %5 === 0){
this.callback && this.callback(this.counter); // ์ฝ๋ฐฑํจ์ ํธ์ถ // counter ์ถ๊ฐ
} // typeScript๊ฐ ์๋์ด์ ์กฐ๊ฑด ์ถ๊ฐ
}
}
// ์ถ๊ฐํ ์ฝ๋ฐฑํจ์ ์ ์ธ
function printSomething(num) { // ์ธ์ ์ถ๊ฐ
console.log(`yo! ${num}`)
}
// ๋ ์ถ๊ฐํ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ฝ๋ฐฑํจ์ ์ ์ธ
function alertNum(num) { // ์ธ์ ์ถ๊ฐ
alert(`wow! ${num}`)
}
// const coolCounter = new Counter(printSomething); // Counter ๋ง๋ค ๋, ์ฝ๋ฐฑํจ์๋ฅผ ๋ฃ์ด์ ์์ฑํด์ฃผ์ด์ผ ํจ
const alertCounter = new Counter(alertNum);
const printCounter = new Counter(printSomething);
// ์
๋ง์ ๋ฐ๋ผ ์ฌ์ฌ์ฉ ๊ฐ๋ฅ
// increase ๋ถ๋ฅผ๋๋ง๋ค ์ฝ๋ฐฑํจ์๋ฅผ ๋ถ๋ฅธ๋ค
coolCounter.increase(); // counter : 1
coolCounter.increase(); // counter : 2
coolCounter.increase(); // counter : 3
coolCounter.increase(); // counter : 4
coolCounter.increase(); // counter : 5
// yo! 5
coolCounter.increase(); // counter : 6
coolCounter.increase(); // counter : 7
coolCounter.increase(); // counter : 8
coolCounter.increase(); // counter : 9
coolCounter.increase(); // counter : 10
// yo! 10
'javaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[js] ์๋ฐ์คํฌ๋ฆฝํธ ๋์ ๊ตฌ์กฐ (0) | 2022.12.06 |
---|---|
JavaScript ๋ฐ๋ณต๋ฌธ (0) | 2021.04.22 |