28 반사 (Reflection)

28.1 Reflect 객체 (The Reflect Object)

Reflect 객체:

  • %Reflect%이다.
  • 전역 객체"Reflect" 프로퍼티 초기 값이다.
  • 일반 객체이다.
  • [[Prototype]] 내부 슬롯 값이 %Object.prototype%이다.
  • 함수 객체가 아니다.
  • [[Construct]] 내부 메서드가 없다; new 연산자로 생성자로 사용할 수 없다.
  • [[Call]] 내부 메서드가 없다; 함수로 호출될 수 없다.

28.1.1 Reflect.apply ( target, thisArgument, argumentsList )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. IsCallable(target) 이 false이면 TypeError 예외를 던진다.
  2. args 를 ? CreateListFromArrayLike(argumentsList)로 둔다.
  3. PrepareForTailCall()을 수행한다.
  4. Call(target, thisArgument, args)를 반환한다.

28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. IsConstructor(target) 가 false이면 TypeError 예외를 던진다.
  2. newTarget 이 존재하지 않으면 newTargettarget 으로 설정한다.
  3. 아니고 IsConstructor(newTarget) 이 false이면 TypeError 예외를 던진다.
  4. args 를 ? CreateListFromArrayLike(argumentsList)로 둔다.
  5. Construct(target, args, newTarget) 를 반환한다.

28.1.3 Reflect.defineProperty ( target, propertyKey, attributes )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. key 를 ? ToPropertyKey(propertyKey) 로 둔다.
  3. desc 를 ? ToPropertyDescriptor(attributes) 로 둔다.
  4. target.[[DefineOwnProperty]](key, desc) 를 반환한다.

28.1.4 Reflect.deleteProperty ( target, propertyKey )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. key 를 ? ToPropertyKey(propertyKey) 로 둔다.
  3. target.[[Delete]](key) 를 반환한다.

28.1.5 Reflect.get ( target, propertyKey [ , receiver ] )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. key 를 ? ToPropertyKey(propertyKey) 로 둔다.
  3. receiver 가 존재하지 않으면
    1. receivertarget 으로 설정한다.
  4. target.[[Get]](key, receiver) 를 반환한다.

28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. key 를 ? ToPropertyKey(propertyKey) 로 둔다.
  3. desc 를 ? target.[[GetOwnProperty]](key) 로 둔다.
  4. FromPropertyDescriptor(desc) 를 반환한다.

28.1.7 Reflect.getPrototypeOf ( target )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. target.[[GetPrototypeOf]]() 를 반환한다.

28.1.8 Reflect.has ( target, propertyKey )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. key 를 ? ToPropertyKey(propertyKey) 로 둔다.
  3. target.[[HasProperty]](key) 를 반환한다.

28.1.9 Reflect.isExtensible ( target )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. target.[[IsExtensible]]() 를 반환한다.

28.1.10 Reflect.ownKeys ( target )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. keys 를 ? target.[[OwnPropertyKeys]]() 로 둔다.
  3. CreateArrayFromList(keys) 를 반환한다.

28.1.11 Reflect.preventExtensions ( target )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. target.[[PreventExtensions]]() 를 반환한다.

28.1.12 Reflect.set ( target, propertyKey, V [ , receiver ] )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. key 를 ? ToPropertyKey(propertyKey) 로 둔다.
  3. receiver 가 존재하지 않으면
    1. receivertarget 으로 설정한다.
  4. target.[[Set]](key, V, receiver) 를 반환한다.

28.1.13 Reflect.setPrototypeOf ( target, proto )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. target 이 Object 가 아니면 TypeError 예외를 던진다.
  2. proto 가 Object 가 아니고 null 도 아니면 TypeError 예외를 던진다.
  3. target.[[SetPrototypeOf]](proto) 를 반환한다.

28.1.14 Reflect [ %Symbol.toStringTag% ]

%Symbol.toStringTag% 프로퍼티 초기 값은 문자열 "Reflect" 이다.

이 프로퍼티의 특성은 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }이다.

28.2 Proxy 객체 (Proxy Objects)

28.2.1 Proxy 생성자 (The Proxy Constructor)

Proxy 생성자:

  • %Proxy%이다.
  • 전역 객체"Proxy" 프로퍼티 초기 값이다.
  • 생성자로 호출될 때 새 Proxy 객체를 생성·초기화한다.
  • 함수로 호출하도록 의도되지 않았으며 그렇게 호출하면 예외를 던진다.

28.2.1.1 Proxy ( target, handler )

이 함수는 호출될 때 다음 단계를 수행한다:

  1. NewTarget 이 undefined 이면 TypeError 예외를 던진다.
  2. ProxyCreate(target, handler) 를 반환한다.

28.2.2 Proxy 생성자의 프로퍼티 (Properties of the Proxy Constructor)

Proxy 생성자:

  • [[Prototype]] 내부 슬롯 값이 %Function.prototype%이다.
  • "prototype" 프로퍼티가 없다 (Proxy 객체는 초기화가 필요한 [[Prototype]] 내부 슬롯을 갖지 않는다).
  • 다음 프로퍼티들을 가진다:

28.2.2.1 Proxy.revocable ( target, handler )

이 함수는 취소(revocable) 가능한 Proxy 객체를 생성한다.

호출 시 다음 단계를 수행한다:

  1. proxy 를 ? ProxyCreate(target, handler) 로 둔다.
  2. revokerClosure 를 아무 것도 포획하지 않고 매개변수 없는 새로운 Abstract Closure 로 두어 호출 시 다음 단계를 수행하게 한다:
    1. F 를 활성 함수 객체로 둔다.
    2. pF.[[RevocableProxy]] 로 둔다.
    3. pnull 이면 NormalCompletion(undefined) 를 반환한다.
    4. F.[[RevocableProxy]]null 로 설정한다.
    5. Assert: p 는 Proxy 특이(exotic) 객체이다.
    6. p.[[ProxyTarget]]null 로 설정한다.
    7. p.[[ProxyHandler]]null 로 설정한다.
    8. NormalCompletion(undefined) 를 반환한다.
  3. revokerCreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] ») 로 둔다.
  4. revoker.[[RevocableProxy]]proxy 로 설정한다.
  5. resultOrdinaryObjectCreate(%Object.prototype%) 로 둔다.
  6. CreateDataPropertyOrThrow(result, "proxy", proxy) 를 수행한다.
  7. CreateDataPropertyOrThrow(result, "revoke", revoker) 를 수행한다.
  8. result 를 반환한다.

28.3 모듈 네임스페이스 객체 (Module Namespace Objects)

모듈 네임스페이스 객체는 모듈의 내보낸 바인딩에 속성 기반 런타임 접근을 제공하는 모듈 네임스페이스 특이 객체이다. 이를 위한 생성자 함수는 없다. 대신 NameSpaceImport 를 포함하는 ImportDeclaration 으로 가져온 각 모듈마다 이러한 객체가 생성된다.

10.4.6 에 명시된 프로퍼티 외에 각 모듈 네임스페이스 객체는 다음 자체 프로퍼티를 가진다:

28.3.1 %Symbol.toStringTag%

%Symbol.toStringTag% 프로퍼티 초기 값은 문자열 "Module" 이다.

이 프로퍼티 특성은 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }이다.