예상할 수 있는 에러와 예상치 못한 에러를 구분 예상치 못한 에러를 대비해 처리하기 위해
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 new Error('에러메세지')
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 클래스를 사용해 만들어 질 객체 자신을 의미 뒤에는 객체의 속성
프로퍼티 값이 함수 일 경우 메서드라 부른다. -> 객체에 묶여 있는 함수