Notice
Recent Posts
Recent Comments
Link
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฑ dreaming DiNO

[JS] ํด๋ž˜์Šค ์˜ˆ์ œ์™€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ •๋ฆฌ ๋ณธ๋ฌธ

javaScript

[JS] ํด๋ž˜์Šค ์˜ˆ์ œ์™€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ •๋ฆฌ

MK_____ 2022. 4. 6. 15:54
 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