26 Reflection

26.1 The Reflect Object

The Reflect object is the %Reflect% intrinsic object and the initial value of the Reflect property of the global object.The Reflect object is an ordinary object.

The value of the [[Prototype]] internal slot of the Reflect object is the intrinsic object %ObjectPrototype% (19.1.3).

The Reflect object is not a function object. It does not have a [[Construct]] internal method; it is not possible to use the Reflect object as a constructor with the new operator. The Reflect object also does not have a [[Call]] internal method; it is not possible to invoke the Reflect object as a function.

26.1.1 Reflect.apply ( target, thisArgument, argumentsList )

When the apply function is called with arguments target, thisArgument, and argumentsList the following steps are taken:

  1. If IsCallable(target) is false, throw a TypeError exception.
  2. Let args be CreateListFromArrayLike(argumentsList).
  3. ReturnIfAbrupt(args).
  4. Perform PrepareForTailCall().
  5. Return Call(target, thisArgument, args).

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

When the construct function is called with arguments target, argumentsList, and newTarget the following steps are taken:

  1. If IsConstructor(target) is false, throw a TypeError exception.
  2. If newTarget is not present, let newTarget be target.
  3. Else, if IsConstructor(newTarget) is false, throw a TypeError exception.
  4. Let args be CreateListFromArrayLike(argumentsList).
  5. ReturnIfAbrupt(args).
  6. Return Construct(target, args, newTarget).

The length property of the construct function is 2.

26.1.3 Reflect.defineProperty ( target, propertyKey, attributes )

When the defineProperty function is called with arguments target, propertyKey, and attributes the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let key be ToPropertyKey(propertyKey).
  3. ReturnIfAbrupt(key).
  4. Let desc be ToPropertyDescriptor(attributes).
  5. ReturnIfAbrupt(desc).
  6. Return target.[[DefineOwnProperty]](key, desc).

26.1.4 Reflect.deleteProperty ( target, propertyKey )

When the deleteProperty function is called with arguments target and propertyKey, the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let key be ToPropertyKey(propertyKey).
  3. ReturnIfAbrupt(key).
  4. Return target.[[Delete]](key).

26.1.5 Reflect.enumerate ( target )

When the enumerate function is called with argument target the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Return target.[[Enumerate]]().

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

When the get function is called with arguments target, propertyKey, and receiver the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let key be ToPropertyKey(propertyKey).
  3. ReturnIfAbrupt(key).
  4. If receiver is not present, then
    1. Let receiver be target.
  5. Return target.[[Get]](key, receiver).

The length property of the get function is 2.

26.1.7 Reflect.getOwnPropertyDescriptor ( target, propertyKey )

When the getOwnPropertyDescriptor function is called with arguments target and propertyKey, the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let key be ToPropertyKey(propertyKey).
  3. ReturnIfAbrupt(key).
  4. Let desc be target.[[GetOwnProperty]](key).
  5. ReturnIfAbrupt(desc).
  6. Return FromPropertyDescriptor(desc).

26.1.8 Reflect.getPrototypeOf ( target )

When the getPrototypeOf function is called with argument target the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Return target.[[GetPrototypeOf]]().

26.1.9 Reflect.has ( target, propertyKey )

When the has function is called with arguments target and propertyKey, the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let key be ToPropertyKey(propertyKey).
  3. ReturnIfAbrupt(key).
  4. Return target.[[HasProperty]](key).

26.1.10 Reflect.isExtensible (target)

When the isExtensible function is called with argument target the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Return target.[[IsExtensible]]().

26.1.11 Reflect.ownKeys ( target )

When the ownKeys function is called with argument target the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let keys be target.[[OwnPropertyKeys]]().
  3. ReturnIfAbrupt(keys).
  4. Return CreateArrayFromList(keys).

26.1.12 Reflect.preventExtensions ( target )

When the preventExtensions function is called with argument target, the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Return target.[[PreventExtensions]]().

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

When the set function is called with arguments target, V, propertyKey, and receiver the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. Let key be ToPropertyKey(propertyKey).
  3. ReturnIfAbrupt(key).
  4. If receiver is not present, then
    1. Let receiver be target.
  5. Return target.[[Set]](key, V, receiver).

The length property of the set function is 3.

26.1.14 Reflect.setPrototypeOf ( target, proto )

When the setPrototypeOf function is called with arguments target and propertyKey, the following steps are taken:

  1. If Type(target) is not Object, throw a TypeError exception.
  2. If Type(proto) is not Object and proto is not null, throw a TypeError exception
  3. Return target.[[SetPrototypeOf]](proto).

26.2 Proxy Objects

26.2.1 The Proxy Constructor

The Proxy constructor is the %Proxy% intrinsic object and the initial value of the Proxy property of the global object. When called as a constructor it creates and initializes a new proxy exotic object. Proxy is not intended to be called as a function and will throw an exception when called in that manner.

26.2.1.1 Proxy ( target, handler )

When Proxy is called with arguments target and handler performs the following steps:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Return ProxyCreate(target, handler).

26.2.2 Properties of the Proxy Constructor

The value of the [[Prototype]] internal slot of the Proxy constructor is the intrinsic object %FunctionPrototype% (19.2.3).

The Proxy constructor does not have a prototype property because proxy exotic objects do not have a [[Prototype]] internal slot that requires initialization.

Besides the length property (whose value is 2), the Proxy constructor has the following properties:

26.2.2.1 Proxy.revocable ( target, handler )

The Proxy.revocable function is used to create a revocable Proxy object. When Proxy.revocable is called with arguments target and handler the following steps are taken:

  1. Let p be ProxyCreate(target, handler).
  2. ReturnIfAbrupt(p).
  3. Let revoker be a new built-in function object as defined in 26.2.2.1.1.
  4. Set the [[RevocableProxy]] internal slot of revoker to p.
  5. Let result be ObjectCreate(%ObjectPrototype%).
  6. Perform CreateDataProperty(result, "proxy", p).
  7. Perform CreateDataProperty(result, "revoke", revoker).
  8. Return result.

26.2.2.1.1 Proxy Revocation Functions

A Proxy revocation function is an anonymous function that has the ability to invalidate a specific Proxy object.

Each Proxy revocation function has a [[RevocableProxy]] internal slot.

When a Proxy revocation function, F, is called the following steps are taken:

  1. Let p be the value of F’s [[RevocableProxy]] internal slot.
  2. If p is null, return undefined.
  3. Set the value of F’s [[RevocableProxy]] internal slot to null.
  4. Assert: p is a Proxy object.
  5. Set the [[ProxyTarget]] internal slot of p to null.
  6. Set the [[ProxyHandler]] internal slot of p to null.
  7. Return undefined.

26.3 Module Namespace Objects

A Module Namespace Object is a module namespace exotic object that provides runtime property-based access to a module’s exported bindings. There is no constructor function for Module Namespace Objects. Instead, such an object is created for each module that is imported by an ImportDeclaration that includes a NameSpaceImport (See 15.2.2).

In addition to the properties specified in 9.4.6 each Module Namespace Object has the own following properties:

26.3.1 @@toStringTag

The initial value of the @@toStringTag property is the String value "Module".

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

26.3.2 [ @@iterator ] ( )

When the @@iterator method is called with no arguments, the following steps are taken:

  1. Let N be the this value.
  2. If Type(N) is not Object, throw a TypeError exception.
  3. Return N.[[Enumerate]]().

The value of the name property of this function is "[Symbol.iterator]".