ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Javascript] ๋ธ”๋ก๋ฌธ, ์Šค์ฝ”ํ”„, scope, if,else,switch,์ง€์—ญ๋ณ€์ˆ˜,์ „์—ญ๋ณ€์ˆ˜,local,global
    ์›น/JavaScript 2023. 1. 10. 12:52

    ๋ธ”๋ก๋ฌธ

    ๐Ÿ‘‰ MDN ๋ฌธ์„œ ๋ณด๊ธฐ

    • 0๊ฐœ ์ด์ƒ์˜ ๋ฌธstatement๋“ค์„ ๋ฌถ์€ ๋‹จ์œ„
    • ์ผ๋ฐ˜์ ์œผ๋กœ, ์ดํ›„ ๋ฐฐ์šธ ์ œ์–ด๋ฌธ, ํ•จ์ˆ˜ ๋“ฑ์— ์‚ฌ์šฉ
    • ์ƒˆ๋กœ์šด ์Šค์ฝ”ํ”„ ์ƒ์„ฑ

     

    ์Šค์ฝ”ํ”„ scope

    ๋ธ”๋ก ์•ˆ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜๋ฅผ ๋ฐ–์—์„œ ์‚ฌ์šฉ ๋ถˆ๊ฐ€

    {
    const x = 'Hello';
    let y = 'world!';
    console.log(x, y);
    }
    โ€‹
    console.log(x);
    console.log(y);

     

    ๋ธ”๋ก ์•ˆ์ชฝ์—์„œ๋Š” ๋ฐ”๊นฅ์˜ ๊ฒƒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

    let x = 1;
    โ€‹
    {
    let y = 2;
    console.log(x, y);
    }
    console.log(x);
    console.log(y);

     

    ๋ธ”๋ก ์•ˆ์ชฝ์— ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๊ฐ€ ์ƒˆ๋กœ ์„ ์–ธ๋˜๋ฉด ๋ฐ”๊นฅ ๊ฒƒ์„ ๋ฎ์–ด์”€

    const xx = 0;
    let yy = 'Hello!';
    console.log(xx, yy);
    โ€‹
    {
    const xx = 1; // ๐Ÿ’ก ๋ธ”๋ก ์•ˆ์—์„œ๋Š” ๋ฐ”๊นฅ์˜ const ์žฌ์„ ์–ธ ๊ฐ€๋Šฅ
    let yy = '์•ˆ๋…•ํ•˜์„ธ์š”~';
    โ€‹
    console.log(xx, yy);
    // โš ๏ธ const, let์„ ๋นผ๋จน์œผ๋ฉด ์žฌ์„ ์–ธ์ด ์•„๋‹ˆ๋ผ ๋ฐ”๊นฅ๊ฒƒ์˜ ๊ฐ’์„(๋ณ€์ˆ˜๋ฉด) ๋ฐ”๊ฟˆ!
    }
    โ€‹
    console.log(xx, yy);



    ์Šค์ฝ”ํ”„ ์ฒด์ธ scope chain

    ๐Ÿฅž ์Šคํƒ stack

    • ํ›„์ž…์„ ์ถœ - ๋‚˜์ค‘์— ์˜จ ๊ฒƒ์ด ๋จผ์ € ๋‚˜๊ฐ

    let a = 0;
    let b = 1;
    let c = 2;
    console.log('์‹œ์  1:', a, b, c);
    โ€‹
    {
    let a = 'A';
    let b = 'B'
    console.log('์‹œ์  2:', a, b, c);
    โ€‹
    {
    let a = '๊ฐ€'
    console.log('์‹œ์  3:', a, b, c);
    }
    โ€‹
    console.log('์‹œ์  4:', a, b, c);
    }
    โ€‹
    console.log('์‹œ์  5:', a, b, c);
    • ๋ธ”๋Ÿญ ์•ˆ์— ํ•ด๋‹น ๋ณ€์ˆ˜/์ƒ์ˆ˜๊ฐ€ ์—†์œผ๋ฉด ๋ฐ”๊นฅ์ชฝ์œผ๋กœ ์ฐพ์•„ ๋‚˜๊ฐ - ์ฒด์ด๋‹

     

    โญ ๋ฉ”๋ชจ๋ฆฌ์—์„œ์˜ ๊ด€์ 

    ์ „์—ญ global ๋ณ€์ˆ˜/์ƒ์ˆ˜

    • ๋ฐ์ดํ„ฐ data ์˜์—ญ์— ์œ„์น˜
    • ์ฝ”๋“œ ์–ด๋Š ๊ณณ์—์„œ๋“  ์ ‘๊ทผ ๊ฐ€๋Šฅ - ๋ธ”๋ก ๋‚ด ๋™๋ช…์˜ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ๋ฎ์–ด์”Œ์›Œ์ง€์ง€ ์•Š๋Š” ์ด์ƒ
    • ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ์‹œ ์†Œ๋ฉธ
    • โญ๏ธ ์ „์—ญ๋ณ€์ˆ˜ ์‚ฌ์šฉ์€ ์ตœ์†Œํ™”ํ•  ๊ฒƒ

    ์ง€์—ญ local ๋ณ€์ˆ˜/์ƒ์ˆ˜

    • ์Šคํƒ ์˜์—ญ์— ์œ„์น˜
    • ํ•ด๋‹น ๋ธ”๋ก ์•ˆ์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ธ”๋ก ์‹คํ–‰ ์ข…๋ฃŒ์‹œ ์†Œ๋ฉธ
    • โญ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๋Š” ๊ฐ€๋Šฅํ•œ ์‚ฌ์šฉํ•  ๋ธ”๋ก ๋‚ด์—์„œ ์„ ์–ธ - ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ

    if๋ฌธ

    const open = true;
    โ€‹
    // ํ•œ์ค„ ์ฝ”๋“œ
    if (open) console.log('์˜์—…์ค‘์ž…๋‹ˆ๋‹ค.');

     

    // ์—ฌ๋Ÿฌ์ค„ ์ฝ”๋“œ - ๋ธ”๋ก๋ฌธ ์‚ฌ์šฉ
    if (open) {
    console.log('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.');
    console.log('์ฆ๊ฑฐ์šด ์‡ผํ•‘ํ•˜์„ธ์š”!');
    }

     

    if ~ else๋ฌธ

    const x = 20;
    โ€‹
    if (x % 2) {
    console.log('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
    } else {
    console.log('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }

    ์ค‘์ฒฉ ์‚ฌ์šฉ

    const x = 22;
    โ€‹
    if (x % 4) {
    if (x % 2) {
    console.log('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
    } else {
    console.log('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }
    } else {
    console.log('4์˜ ๋ฐฐ์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }

     

    if ~ else if ~๋ฌธ

    const x = 21;
    โ€‹
    if (x % 2) {
    console.log('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
    } else if (x % 4) {
    console.log('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
    } else {
    console.log('4์˜ ๋ฐฐ์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }



    ๐Ÿ’ก ๋ณด๋‹ค ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ์‹

    • return๋ฌธ: ํ•จ์ˆ˜ ์‹คํ–‰์„ ์™„์ „ํžˆ ์ข…๋ฃŒ
    function evalNum () {
    const x = 21;
    โ€‹
    if (x % 2) {
    console.log('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
    return;
    }
    โ€‹
    if (x % 4) {
    console.log('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
    return;
    }
    โ€‹
    console.log('4์˜ ๋ฐฐ์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }
    โ€‹
    evalNum();
    console.log('๋ธ”๋ก๋ฌธ ๋ฐ”๊นฅ');

     

    ํŠน์ • ๊ฐ’์— ๋Œ€ํ•œ ๋‹ค์ˆ˜์˜ ์˜ต์…˜์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉ

     

    ์ฃผ์–ด์ง„ ํ‰๊ฐ€์— ์ผ์น˜ํ•˜๋Š” case๋กœ ์‹คํ–‰์œ„์น˜ ์ด๋™

    const fingersOut = 2;
    โ€‹
    switch (fingersOut) {
    // ์ˆœ์„œ ์ƒ๊ด€์—†์Œ
    case 2:
    console.log('๊ฐ€์œ„');
    break;
    case 0:
    console.log('๋ฐ”์œ„');
    break;
    case 5:
    console.log('๋ณด');
    break;
    default:
    console.log('๋ฌดํšจ');
    }
    • break;๋“ค์„ ์ œ๊ฑฐํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณผ ๊ฒƒ
    • default: ๋งจ ์•„๋ž˜ ์ž‘์„ฑ, break๋˜์ง€ ์•Š์€ ์ด์ƒ ๋ฌด์กฐ๊ฑด ์‹คํ–‰

     

    const direction = 'north'
    let directionKor;
    โ€‹
    switch (direction) {
    case 'north':
    directionKor = '๋ถ';
    break;
    case 'south':
    directionKor = '๋‚จ';
    break;
    case 'east':
    directionKor = '๋™';
    break;
    case 'west':
    directionKor = '์„œ';
    break;
    default:
    directionKor = '๋ฌดํšจ';
    }
    โ€‹
    console.log(directionKor);
    • break;๋“ค์„ ์ œ๊ฑฐํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณผ ๊ฒƒ



    // ๐Ÿ’ก ์ฐธ๊ณ : ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•
    const direction = 'north'
    โ€‹
    const directionKor = {
    north: '๋ถ',
    south: '๋‚จ',
    east: '๋™',
    west: '์„œ'
    }[direction] ?? '๋ฌดํšจ'
    โ€‹
    console.log(directionKor);




    const month = 1;
    let season = '';
    โ€‹
    switch (month) {
    case 1: case 2: case 3:
    season = '1๋ถ„๊ธฐ'; break;
    โ€‹
    case 4: case 5: case 6:
    season = '2๋ถ„๊ธฐ'; break;
    โ€‹
    case 7: case 8: case 9:
    season = '3๋ถ„๊ธฐ'; break;
    โ€‹
    case 10: case 11: case 12:
    season = '4๋ถ„๊ธฐ'; break;
    โ€‹
    default:
    season = '์ž˜๋ชป๋œ ์›”์ž…๋‹ˆ๋‹ค.';
    }
    โ€‹
    console.log(season);

     

    const startMonth = 1;
    let holidays = '๋ถ„๊ธฐ ๋‚ด ํœด์ผ:';
    โ€‹
    switch (startMonth) {
    case 1:
    holidays += ' ์„ค๋‚ ';
    case 2:
    case 3:
    holidays += ' 3•1์ ˆ';
    break;
    โ€‹
    case 4:
    case 5:
    holidays += ' ์–ด๋ฆฐ์ด๋‚ ';
    case 6:
    holidays += ' ํ˜„์ถฉ์ผ';
    break;
    โ€‹
    case 7:
    case 8:
    holidays += ' ๊ด‘๋ณต์ ˆ';
    case 9:
    holidays += ' ์ถ”์„';
    break;
    โ€‹
    case 10:
    holidays += ' ํ•œ๊ธ€๋‚ ';
    case 11:
    case 12:
    holidays += ' ํฌ๋ฆฌ์Šค๋งˆ์Šค';
    break;
    โ€‹
    default:
    holidays = '์ž˜๋ชป๋œ ์›”์ž…๋‹ˆ๋‹ค.';
    }
    โ€‹
    console.log(holidays);
     
     
Designed by Tistory.