JavaScript
빌트인 객체, 래퍼 객체
foxlee
2021. 12. 17. 16:24
자바스크립트의 3가지 객체
- 표준 빌트인 객체
- Object, String, Number, Boolean, Symbol, Date, Math, Array, Promise 등등
- 사용 환경에 상관 없이 사용 가능
- 호스트 객체
- 브라우저 환경 DOM, Canvas, XMLHttpRequest, fetch 등
- 노드 환경 - 노드 고유의 API
- 사용자가 정의한 객체
원시값과 래퍼 객체
const strObj = new String('Lee'); // String {"Lee"}
console.log(Object.getPrototypeOf(strObj) === String.prototype); // true // String 생성자 함수를 통해 생성한 strObj 객체의 프로토타입은 String.prototype이다.
console.log(typeof strObj); // object
console.log(strObj.length) // 3 //
console.log(strObj.toUpperCase()) // LEE
const str = 'hello'; // 원시 타입의 문자열
console.log(str.length); // 5 // String 객체의 프로퍼티와 메서드를 갖고 있는 객체처럼 동작
console.log(str.toUpperCase()); // HELLO
console.log(Number.isInteger(1)) // 1 // 인스턴스 없이 호출할 수 있는 정적 메서드
- const str = 'hello' 에서 str.length 메서드를 호출할 수 있다 => 자바스크립 엔진이 str를 일시적으로 연관된 객체로 변환해줌.(임시 객체, 래퍼 객체)
- 일시적으로 생성된 객체는 식별자가 원래 상태로 돌아가면(위의 경우 String 객체 -> 원시 형태 문자열로 돌아옴) 가비지 컬렉션 대상이 됨