원시 타입 o | 원시 타입 x |
---|---|
숫자 - 정수, 64비트 부동 소수점 | 함수 - 재사용 가능한 코드 블록 |
문자열 - '작은 따옴표' , "큰 따옴표" , 백틱 ` | 배열 - 순서 있는 값들의 리스트 |
불리언 - true, false | 정규표현식 - 문자열에서 패턴을 검색하거나 대체 |
NULL - 값이 없음 | 날짜 - data (날짜와 시간) |
Undefined - 값이 할당되지 않은 변수 | Error - 예외 처리 |
Symbol - 유일한 식별자를 생성 |
원시 타입을 제외한 나머지 값들은 모두 객체
키(key), 값(value)으로 구성된 속성(property)의 집합
예제 ✔️
// 객체 리터럴을 사용하여 객체 생성 let person = { name: 'John', age: 30, city: 'New York', getInfo: function() { return `${this.name} is ${this.age} years old and lives in ${this.city}.`; } }; // 객체 속성에 접근 console.log(person.name); // 출력: John console.log(person.age); // 출력: 30 // 객체 메서드 호출 console.log(person.getInfo()); // 출력: John is 30 years old and lives in New York.
객체 : person
키(객체의 속성) : name, age, city, getInfo
값(각각의 키에 할당된 값) : 'John', 30, 'New York'
객체 리터럴 : 중괄호 {}로 둘러싸인 부분,
예제 ✔️
// 생성자 함수 정의 function Person(name, age) { this.name = name; this.age = age; } // 새로운 인스턴스 생성 let john = new Person('John', 30); // john 객체 확인 console.log(john.name); // 출력: John console.log(john.age); // 출력: 30
// 클래스 선언 class Car { constructor(brand, model) { //클래스의 생성자(객체 생성될 때 초기화 ) this.brand = brand; this.model = model; } getCarInfo() { //자동차 정보를 반환 return `${this.brand} ${this.model}`; } } // 인스턴스 생성 let myCar = new Car('Toyota', 'Corolla'); // 메서드 호출 console.log(myCar.getCarInfo()); // 출력: Toyota Corolla