Node.js 입문 1주차 -2

Han Lee·2022년 12월 12일
0

Node.js 입문

목록 보기
2/5

Error handling

예상할 수 있는 에러와 예상치 못한 에러를 구분 예상치 못한 에러를 대비해 처리하기 위해

try/catch - 에러가 발생하지 않게 하기 위해 예외처리

const users = ["Lee", "Kim", "Park", 2];

try {
  for (const user of users) {
    console.log(user.toUpperCase());
  }
} catch (err) {
  console.error(`Error: ${err.message}`);
}

// LEE
// KIM
// PARK
// Error: user.toUpperCase is not a function

try/catch문을 쓰지않으면 서버가 종료가 된다.

throw - 에러를 고의로 발생시키는

에러를 발생시키는 이유 : 강제로 서비스를 종료 시켜야 하는 상황

throw new Error('에러메세지')

finally - 성공,실패 상관없이 실행하는

클래스

class User {
  constructor(name, age, tech) { // User 클래스의 생성자
    this.name = name;
    this.age = age;
    this.tech = tech;
  }
}

const user = new User("이용우", 28, "Node.js"); // user 인스턴스 생성

console.log(user.name); // 이용우
console.log(user.age); // 28
console.log(user.tech); // Node.js

user 변수 = 실제 빵 USER 클래스 = 빵틀
생성자 : 클래스 내부에서 constructor()로 정의한 메서드

this와 프로퍼티

this 클래스를 사용해 만들어 질 객체 자신을 의미 뒤에는 객체의 속성

메서드

프로퍼티 값이 함수 일 경우 메서드라 부른다. -> 객체에 묶여 있는 함수

상속

profile
렌덤형 인간

0개의 댓글