Skip to content

Latest commit

Β 

History

History
113 lines (94 loc) Β· 1.74 KB

ch47-문제.md

File metadata and controls

113 lines (94 loc) Β· 1.74 KB

Chapter47

πŸ“Œλ¬Έμ œ1

λ‹€μŒ μ½”λ“œμ˜ μ‹€ν–‰ κ²°κ³Όλ₯Ό μ„œμˆ ν•˜μ‹œμ˜€.

const func = (n, f) => {
  if(typeof f !== 'function') new Error('f must be function');
  f(n);
}

try {
  func(1, 2);
}catch(e){
   console.log(e);
}

λ‹΅μ•ˆ μž‘μ„±



πŸ“Œλ¬Έμ œ2

λ‹€μŒ μ½”λ“œμ˜ μ‹€ν–‰ κ²°κ³Όλ₯Ό μ˜ˆμƒν•΄λ³΄κ³ , μ˜λ„μ— 맞게 κ³ μΉ˜μ„Έμš”.

const practice = (n, f) => {
  if (typeof f !== "function") {
    new TypeError("f must be a function");
  }
};

try {
  practice(2, 1);
  new Error("something wrong");
} catch (error) {
  console.log(error);
}

λ‹΅μ•ˆ μž‘μ„±



πŸ“Œλ¬Έμ œ3

λ‹€μŒ μ½”λ“œμ˜ (1)μ—μ„œ thisκ°€ 무엇일지 μž‘μ„±ν•˜κ³ , (2)전체 μ‹€ν–‰ 좜λ ₯ κ²°κ³Όλ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

const obj = {
  name: 'i am object'
};

async function test2() {
  try {
    throw 123;
  } catch (error) {
    // (1) this
    console.log('test2-catch');
    throw new Error('test2-catch');
  }
}

function test1() {
  try {
    test2.bind(obj)();
    console.log('test1-try');
  } catch (error) {
    console.log('test1-catch');
    throw new Error('test1-catch');
  }
}

try {
  test1()
} catch (error) {
  console.log(error);
}

λ‹΅μ•ˆ μž‘μ„±

(1) : 
(2) : 

πŸ“Œλ¬Έμ œ4

f1을 μ‹€ν–‰μ‹œμΌ°μ„λ•Œ λ°œμƒν•˜λŠ” μ—λŸ¬λ₯Ό μ²˜λ¦¬ν•˜λŠ” μ½”λ“œλ₯Ό f1ν•¨μˆ˜λ‚΄λΆ€μ˜ catchλ¬Έμ—μ„œ μž‘μ„±ν•˜κ³ , μ΄μ™Έμ˜ μ—λŸ¬λŠ” "μ•Œμˆ˜ μ—†λŠ” μ—λŸ¬ λ°œμƒ" 으둜 μ²˜λ¦¬ν•˜λ„λ‘ μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ„Έμš”.

    const f1 = () => {
        try {
          console.log(b);
        } catch (error) {
      // (1)
        }
      };

      try {
        f1();
      } catch (error) {
        console.error("μ•Œμˆ˜ μ—†λŠ” μ—λŸ¬ λ°œμƒ");
      }

λ‹΅μ•ˆ μž‘μ„±

(1)