ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Javascript] λ§€κ°œλ³€μˆ˜ ,μ€‘μ²©ν•¨μˆ˜, μž¬κ·€ν•¨μˆ˜, factorial, μ¦‰μ‹œμ‹€ν–‰ν•¨μˆ˜,IIFE
    μ›Ή/JavaScript 2023. 1. 11. 06:52

    πŸ’‘ ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜ 갯수λ₯Ό λ„˜μ–΄κ°€λŠ” 인수

    • 였λ₯˜λ₯Ό μΌμœΌν‚€μ§€ μ•Šκ³  λ¬΄μ‹œλ¨
    function add(a, b) {
    return a + b;
    }
    ​
    console.log(
    add(1, 3),
    add(1, 3, 5),
    add(1, 3, 5, 7)
    );




     

    I. κΈ°λ³Έκ°’ λ§€κ°œλ³€μˆ˜ default parameter

    πŸ‘‰ MDN λ¬Έμ„œ 보기

    function add(a = 2, b = 4) {
    console.log(`${a} + ${b}`);
    return a + b;
    }
    ​
    console.log(
    add(),
    add(1),
    add(1, 3)
    );




     

    II. arguments - ν•¨μˆ˜ λ‚΄μ—μ„œ μ‚¬μš©κ°€λŠ₯ν•œ 지역 λ³€μˆ˜

    πŸ‘‰ MDN λ¬Έμ„œ 보기

    • λ°°μ—΄μ˜ ν˜•νƒœλ₯Ό ν•œ κ°μ²΄ - λ°°μ—΄μ€ μ•„λ‹ˆμ§€λ§Œ 사싀상 λ°°μ—΄μ²˜λŸΌ λ™μž‘ (배열도 사싀 κ°μ²΄μ§€λ§Œ...)
    • ν•¨μˆ˜ ν˜ΈμΆœ μ‹œ μ „λ‹¬λœ λͺ¨λ“  μΈμˆ˜λ“€μ„ λ°°μ—΄ ν˜•νƒœλ‘œ 가짐
    function add(a, b) {
    console.log('1.', arguments);
    console.log('2.', arguments[0]);
    console.log('3.', typeof arguments);
    return a + b;
    }
    ​
    console.log(
    '4.', add(1, 3, 5, 7)
    );

     

    function add(a, b) {
    for (const arg of arguments) {
    console.log(arg);
    }
    return a + b;
    }
    ​
    console.log(
    add(1, 3, 5, 7)
    );
    • for ... ofκ°€ κ°€λŠ₯ν•œ 이유: iterable이기 λ•Œλ¬Έ μ΄ν›„ λ‹€λ£Έ
    • ⚠️ ν™”μ‚΄ν‘œ ν•¨μˆ˜μ—μ„œλŠ” arguments μ‚¬μš© λΆˆκ°€! ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ λ°”κΎΈμ–΄ μ‹€ν–‰ν•΄ λ³Ό 것

     

    function getAverage() {
    let result = 0;
    for (const num of arguments) {
    result += num;
    }
    return result / arguments.length;
    }
    ​
    console.log(
    getAverage(1, 4, 7),
    getAverage(24, 31, 52, 80)
    );

     

    // πŸ’‘ νƒ€μž…μ— μ•ˆμ „ν•œ 버전
    function getAverage() {
    let result = 0, count = 0;
    for (const num of arguments) {
    if (typeof num !== 'number') continue;
    result += num;
    count++;
    }
    return result / count;
    }
    ​
    console.log(
    getAverage(2, 'κ°€', 8, true, 5)
    );



    // ♻️ μƒˆλ‘œκ³ μΉ¨ ν›„ μ‹€ν–‰
    const add = (a, b) => a + b;
    const sub = (a, b) => a - b;
    const mul = (a, b) => a * b;
    const div = (a, b) => a / b;
    ​
    function combineArms () {
    return (x, y) => {
    let result = x;
    for (const arm of arguments) {
    if (typeof arm !== 'function') continue;
    result = arm(result, y);
    }
    return result;
    }
    }
    ​
    const add_mul = combineArms(add, mul, 1, true);
    const add_mul_sub = combineArms(add, mul, sub);
    const add_mul_sub_div = combineArms(add, mul, sub, div);
    ​
    // πŸ’‘ 읡λͺ… ν•¨μˆ˜ μ‚¬μš©λ¨
    const add_mul_sub_div_pow
    = combineArms(add, mul, sub, div, (x, y) => x ** y);

     

    console.log(
    add_mul(8, 3),
    add_mul_sub(8, 3),
    add_mul_sub_div(8, 3),
    add_mul_sub_div_pow(8, 3)
    );




     

    III. ...λ³€μˆ˜κ·Έλ£Ήλͺ… - λ‚˜λ¨Έμ§€ λ³€μˆ˜ rest parameters

    πŸ‘‰ MDN λ¬Έμ„œ 보기

    • νŠΉμ • λ§€κ°œλ³€μˆ˜λ“€ 뒀에 정해지지 μ•Šμ€ 수의 λ§€κ°œλ³€μˆ˜λ“€μ„ 받을 λ•Œ
    • λ§ˆμ§€λ§‰ μΈμžλ‘œλ§Œ μ‚¬μš© κ°€λŠ₯
    • argumentsμ™€λŠ” 달리 μ‹€μ œ λ°°μ—΄μž„
    console.log(
    '3.',
    classIntro(3, '김민지', '영희', '철수', '보라')
    ); // ν˜Έμ΄μŠ€νŒ…
    ​
    function classIntro (classNo, teacher, ...children) {
    console.log('1.', children);
    console.log('2.', arguments);
    ​
    let childrenStr = '';
    for (const child of children) {
    if (childrenStr) childrenStr += ', ';
    childrenStr += child;
    }
    return `${classNo}반의 μ„ μƒλ‹˜μ€ ${teacher}, `
    + `학생듀은 ${childrenStr}μž…λ‹ˆλ‹€.`
    }

     

    const add = (a, b) => a + b;
    const sub = (a, b) => a - b;
    const mul = (a, b) => a * b;
    const div = (a, b) => a / b;
    ​
    function doMultiArms (x, y, ...arms) {
    let result = x;
    for (const arm of arms) {
    if (typeof arm !== 'function') continue;
    result = arm(result, y);
    }
    return result;
    }
    ​
    console.log(
    doMultiArms(8, 3, add, mul, 1, true),
    doMultiArms(8, 3, add, mul, sub),
    doMultiArms(8, 3, add, mul, sub, div),
    doMultiArms(8, 3, add, mul, sub, div, (x, y) => x ** y)
    );
     

    I. μ€‘μ²©λœ ν•¨μˆ˜

    function outer () {
    const name = 'λ°”κΉ₯μͺ½'
    console.log(name, 'ν•¨μˆ˜');
    ​
    function inner () {
    const name = 'μ•ˆμͺ½'
    console.log(name, 'ν•¨μˆ˜');
    }
    inner();
    }
    ​
    outer();

     

    function addMulSub (x, y) {
    const add = (a, b) => a + b;
    const sub = (a, b) => a - b;
    const mul = (a, b) => a * b;
    ​
    return sub(mul(add(x, y), y), y);
    }
    ​
    console.log(addMulSub(8, 3));




     

    II. μž¬κ·€ ν•¨μˆ˜ recursive function

    function upto5 (x) {
    console.log(x);
    if (x < 5) {
    upto5(x + 1);
    } else {
    console.log('- - -');
    };
    }
    ​
    upto5(1);
    upto5(3);
    upto5(7);

    • μŠ€νƒμ΄ λ„˜μΉ˜λ©΄ stack overflow - 였λ₯˜ λ°œμƒ



    πŸ’‘ νŒ©ν† λ¦¬μ–Ό factorial μž¬κ·€ν•¨μˆ˜

    function fact(x) {
    return x === 0 ? 1 : x * fact(x - 1);
    }
     
    λ˜λŠ”
    function fact(a) {
     return !a ? 1 : a * fact(a-1) ; 
     }
    ​
    console.log(
    fact(1),
    fact(2),
    fact(3),
    fact(4)
    )




     

    III. μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜ IIFE

    πŸ‘‰ MDN λ¬Έμ„œ 보기

    • Immideately Invoked Function Expression
    • μ˜€λŠ˜λ‚ μ—λŠ” 잘 μ‚¬μš©λ˜μ§€ μ•ŠμŒ - κ³Όκ±° μ½”λ“œ 뢄석을 μœ„ν•΄...
    (function () {
    console.log('IIFE');
    })();

    πŸ’‘ 무엇에 μ‚¬μš©λ˜μ—ˆλŠ”κ°€?

    const initialMessage = (function () {
    // ⚠️ varλ₯Ό μ‚¬μš©ν•¨μ— μ£Όλͺ©
    var month = 8;
    var day = 15;
    var temps = [28, 27, 27, 30, 32, 32, 30, 28];
    var avgTemp = 0;
    for (const temp of temps) {
    avgTemp += temp
    }
    avgTemp /= temps.length;
    ​
    return `${month}μ›” ${day}일 ν‰κ· κΈ°μ˜¨μ€ 섭씨 ${avgTemp}λ„μž…λ‹ˆλ‹€.`;
    })();
    ​
    console.log(initialMessage);
    console.log(month);
    • λ”± ν•œ 번만 μ‚¬μš©λ  ν•¨μˆ˜μ—
    • μ „μ—­ λ³€μˆ˜λ“€μ„ μ‚¬μš©ν•˜μ§€ μ•Šκ³ , λ³΅μž‘ν•œ κΈ°λŠ₯을 μΌνšŒμ„±μœΌλ‘œ μ‹€ν–‰ν•  λ•Œ
    • λ‹€λ₯Έ μ½”λ“œλ“€κ³Όμ˜ λ³€μˆ˜λͺ…μ΄λ‚˜ μƒμˆ˜λͺ… μΆ©λŒμ„ 막기 μœ„ν•¨ (특히 λ§Žμ€ μ½”λ“œλ“€μ΄ μ‚¬μš©λ  λ•Œ)
    • μ˜€λŠ˜λ‚ μ—λŠ” 블둝과 이후 배울 λͺ¨λ“ˆμ˜ μ‚¬μš©μœΌλ‘œ λŒ€μ²΄
      • μ΄μ „μ˜ varλŠ” 블둝 μ™Έμ—μ„œ μ‚¬μš©λ  수 μžˆμ—ˆμŒ(‼️)
    let initialMessage;
    ​
    {
    const month = 8;
    const day = 15;
    const temps = [28, 27, 27, 30, 32, 32, 30, 28];
    let avgTemp = 0;
    for (const temp of temps) {
    avgTemp += temp
    }
    avgTemp /= temps.length;
    ​
    initialMessage = `${month}μ›” ${day}일 ν‰κ· κΈ°μ˜¨μ€ 섭씨 ${avgTemp}λ„μž…λ‹ˆλ‹€.`;
    };
    ​
    console.log(initialMessage);
    console.log(month); // μƒˆλ‘œκ³ μΉ¨ ν›„ constλ₯Ό var둜 λ°”κΎΈκ³  μ‹€ν–‰ν•΄λ³Ό 것




     

    IV. ⭐️ λΆˆλ³€μ„± immutability

    let x = 1;
    let y = {
    name: '홍길동',
    age: 15
    }
    let z = [1, 2, 3];
    ​
    function changeValue (a, b, c) {
    a++;
    b.name = 'μ „μš°μΉ˜';
    b.age++;
    c[0]++;
    ​
    console.log(a, b, c);
    }
    ​
    changeValue(x, y, z);

     

    console.log(x, y, z);

    μ›μ‹œ νƒ€μž… : 인자둜 λ“€μ–΄κ°„ ν•¨μˆ˜ λ‚΄μ—μ„œμ˜ 변경에 영ν–₯ 받지 μ•ŠμŒ

    • μ‹€μ œ 값이 μ•„λ‹ˆλΌ λ³΅μ‚¬λœ 값이 λ“€μ–΄κ°”κΈ° λ•Œλ¬Έ

    μ°Έμ‘° νƒ€μž… : 인자둜 λ“€μ–΄κ°„ ν•¨μˆ˜ λ‚΄μ—μ„œ μš”μ†Œκ°€ λ³€ν•˜λ©΄ μ‹€μ œλ‘œλ„ 변함

    • λ³΅μ‚¬λœ 값도 같은 κ°μ²΄λ‚˜ 배열을 가리킀기 λ•Œλ¬Έ

     

    ⭐️ ν•¨μˆ˜μ— 주어진 인자λ₯Ό λ³€κ²½ν•˜λŠ” 것은 쒋지 μ•ŠμŒ

    • ⚠️ μ™ΈλΆ€μ˜ ν™˜κ²½μ„ λ³€κ²½ν•˜λŠ” ν•¨μˆ˜λŠ” μœ„ν—˜!
    • 이상적인 ν•¨μˆ˜: 받은 κ°’λ“€λ§Œ μ²˜λ¦¬ν•˜μ—¬ μƒˆ 값을 λ°˜ν™˜
Designed by Tistory.