28 Reflection

28.1 The Reflect Object

The Reflect object:

  • is %Reflect%.
  • is the initial value of the "Reflect" property of the global object.
  • is an ordinary object.
  • has a [[Prototype]] internal slot whose value is %Object.prototype%.
  • is not a function object.
  • 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.

28.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. Perform PrepareForTailCall().
  4. Return ? Call(target, thisArgument, args).

28.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, set newTarget to target.
  3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
  4. Let args be ? CreateListFromArrayLike(argumentsList).
  5. Return ? Construct(target, args, newTarget).

28.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. Let desc be ? ToPropertyDescriptor(attributes).
  4. Return ? target.[[DefineOwnProperty]](key, desc).

28.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. Return ? target.[[Delete]](key).

28.1.5 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. If receiver is not present, then
    1. Set receiver to target.
  4. Return ? target.[[Get]](key, receiver).

28.1.6 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. Let desc be ? target.[[GetOwnProperty]](key).
  4. Return FromPropertyDescriptor(desc).

28.1.7 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]]().

28.1.8 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. Return ? target.[[HasProperty]](key).

28.1.9 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]]().

28.1.10 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. Return CreateArrayFromList(keys).

28.1.11 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]]().

28.1.12 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. If receiver is not present, then
    1. Set receiver to target.
  4. Return ? target.[[Set]](key, V, receiver).

28.1.13 Reflect.setPrototypeOf ( target, proto )

When the setPrototypeOf function is called with arguments target and proto, 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).

28.1.14 Reflect [ @@toStringTag ]

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

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

28.2 Proxy Objects

28.2.1 The Proxy Constructor

The Proxy constructor:

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

28.2.1.1 Proxy ( target, handler )

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

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

28.2.2 Properties of the Proxy Constructor

The Proxy constructor:

  • has a [[Prototype]] internal slot whose value is %Function.prototype%.
  • does not have a "prototype" property because Proxy objects do not have a [[Prototype]] internal slot that requires initialization.
  • has the following properties:

28.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. Let revokerClosure be a new Abstract Closure with no parameters that captures nothing and performs the following steps when called:
    1. Let F be the active function object.
    2. Let p be F.[[RevocableProxy]].
    3. If p is null, return undefined.
    4. Set F.[[RevocableProxy]] to null.
    5. Assert: p is a Proxy object.
    6. Set p.[[ProxyTarget]] to null.
    7. Set p.[[ProxyHandler]] to null.
    8. Return undefined.
  3. Let revoker be CreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] »).
  4. Set revoker.[[RevocableProxy]] to p.
  5. Let result be OrdinaryObjectCreate(%Object.prototype%).
  6. Perform ! CreateDataPropertyOrThrow(result, "proxy", p).
  7. Perform ! CreateDataPropertyOrThrow(result, "revoke", revoker).
  8. Return result.

28.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 contains a NameSpaceImport.

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

28.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]]: false }.