빌트인 객체에 대해서

·2022년 4월 29일
0

javascript을 보자

목록 보기
2/2

자바스크립트의 객체들

js에서는 크게 3가지로 객채를 분리할 수 있다.

  1. 표준 빌트인 객체: ECMAScript 사양에 정의된 객체를 말한다.ECMAScript에 정의된 객체 이기 때문에, 자바스크립트 실행 환경(브라우저, nodejs) 등에 관계없이 사용 할 수 있다. 전역 객체의 프로퍼티로써 제공된다.
  2. 호스트 객체: ECMAScript에는 정의가 안되어 있지만, 브라우저, Nodejs등 같은 환경에서 추가로 제공해주는 객체이다
  3. 사용자 정의 객체: 사용자가 직접 정의한 객체이다
  4. es6부터는 Exotic Objects 라는 객체가 있는데, 일반 객체와 비슷하지만 조금 다르게 동작한다

표준 빌트인 객체

tc39 ECMAScript Standard Built-in Objects

js는 약 40여개의 빌트인 객체를 제공하고
Math, reflect, JSON을 제왜 하면 모두 인스턴스를 생성 할 수 있는 생성자 함수 객체이다.

const str= new String("test");
console.log(typeof str); // object

생성자 함수인 표준 빌트인 객체가 생성한 인스턴스의 프로토 타입은
표준 빌트인 갹체의 prototype 프로퍼티에 바인딩이 되어있디.

표준 빌트인 객체는 인스턴스가 아니더라고, 호출 가능한 정적 매서드를 제공한다

console.log(Number.isInteger(0.5)) // false

원시값과 래퍼 객체

원시값은 객체가 아니지만 객체처럼 프로퍼티나 매서드에 접근 해서 동작이 가능하다

const str="test"
console.log(str.length)

가능한 이유는 원시값이 객체처럼 . [] 방식으로 프로퍼티나 매서드에 접근하면
js엔진이 일시적으로 원시값을 연관된 타입 객체로 암묵적으로 변환 해준다.

그리고 임시로 변환해주는 객체를 래퍼 객체라고 한다.

const str="test"
위와 같이 string 타입 변수 str 의 경우 . 이나 [] 로 매서드나 프로퍼티에 접근 할 경우
해당 변수는 문자열 래퍼 객체로 변환이 되고 기존 변수의 값은
문자열 래퍼 객체의 [ [stringDtat] ] 슬롯에 할당 된다.

그리고 해당 문자열 래퍼 객체는 string.prototype의 매서드를 상속 받아 사용 할 수 있기 때문에
str.toUpperCase() 같은 형태의 코드가 실행 될 수 있다.

그리고 프로퍼티 접근이다 매서드 호출이 끝나면 해당 래퍼객체는 gc 대상이 되고
기존 원시값으로 되돌아간다.

const str="test"
str.name="hi"
console.log(str.name) // undefined

전역 객체

전역객체란 코드가 실행 되기전에 js엔진에 의해서 어떤 객체보다 먼저 생성되는 객체이고, 어떤객체에도 속하지 않는 최상위 객체이다.

19 The Global Object

is created before control enters any execution context.

does not have a [ [Construct]] internal method; it cannot be used as a constructor with the new operator.

does not have a [[Call]] internal method; it cannot be invoked as a function.

has a [ [Prototype]] internal slot whose value is host-defined.

globalThis.__proto__
Window {TEMPORARY: 0, PERSISTENT: 1, Symbol(Symbol.toStringTag): 'Window', constructor: ƒ}

may have host-defined properties in addition to the properties defined in this specification. This may include a property whose value is the global object itself.


환경에 따라서 브라우저는 window, nodejs에서는 global 이 전역객체를 가리키지만
es11 globalThis를 사용 할 수 있다

19.1.1 globalThis

The initial value of the "globalThis" property of the global object in a Realm Record realm is realm.[[GlobalEnv]].[[GlobalThisValue]].

This property has the attributes { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.

그리고 그외 전역 객체의 프로퍼티 값은
1. Infinity
2. NaN
3. undefined가 있다
각 프로퍼티들은

globalThis.Infinity
Infinity

앞에 globalThis를 쓰지 않아도 접근이 가능하다

전역객체의 매서드들

# Function Properties of the Global Object

tc39에서 상세히 설명을 해주지만

	eval
	isFinite
	isNaN
	parseFloat
	parseInt
	decodeURI
	decodeURIComponent 
	encodeURI
	encodeURIComponent

이 정도가 있다.

암묵적 전역

var x=20

function ff(){
	y=10
}
ff()
console.log(x+y)

console.log(globalThis.y) // 10
console.log(globalThis.x) // undefined

식별자 없이 변수 할당을 하는것이 오류가 날것 같지만, 선언 하지 않은 식별자에 값을 할당하면 전역객체의 프로퍼티로 할당이 된다

console.log(x) // 10
console.log(y) // 10
function f()
    {
        y=10
    }
f()
let x=20

그렇기 때문에 객체의 프로퍼티는 호이스팅이 되지 않는다.

퀴즈

console.log(window.String.toUpperCase) // ? 
console.log(window.JSON) // ?

출처:
https://tc39.es/ecma262/#sec-value-properties-of-the-global-object
자바스크립티 딥다이브 책

0개의 댓글