Proposal to add Metadata to ECMAScript.
// define metadata on an object or property
Reflect.defineMetadata(metadataKey, metadataValue, target);
Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey);
// check for presence of a metadata key on the prototype chain of an object or property
let result = Reflect.hasMetadata(metadataKey, target);
let result = Reflect.hasMetadata(metadataKey, target, propertyKey);
// check for presence of an own metadata key of an object or property
let result = Reflect.hasOwnMetadata(metadataKey, target);
let result = Reflect.hasOwnMetadata(metadataKey, target, propertyKey);
// get metadata value of a metadata key on the prototype chain of an object or property
let result = Reflect.getMetadata(metadataKey, target);
let result = Reflect.getMetadata(metadataKey, target, propertyKey);
// get metadata value of an own metadata key of an object or property
let result = Reflect.getOwnMetadata(metadataKey, target);
let result = Reflect.getOwnMetadata(metadataKey, target, propertyKey);
// get all metadata keys on the prototype chain of an object or property
let result = Reflect.getMetadataKeys(target);
let result = Reflect.getMetadataKeys(target, propertyKey);
// get all own metadata keys of an object or property
let result = Reflect.getOwnMetadataKeys(target);
let result = Reflect.getOwnMetadataKeys(target, propertyKey);
// delete metadata from an object or property
let result = Reflect.deleteMetadata(metadataKey, target);
let result = Reflect.deleteMetadata(metadataKey, target, propertyKey);
// apply metadata via a decorator to a constructor
@Reflect.metadata(metadataKey, metadataValue)
class C {
// apply metadata via a decorator to a method (property)
@Reflect.metadata(metadataKey, metadataValue)
method() {
}
}
// Design-time type annotations
function Type(type) { return Reflect.metadata("design:type", type); }
function ParamTypes(...types) { return Reflect.metadata("design:paramtypes", types); }
function ReturnType(type) { return Reflect.metadata("design:returntype", type); }
// Decorator application
@ParamTypes(String, Number)
class C {
constructor(text, i) {
}
@Type(String)
get name() { return "text"; }
@Type(Function)
@ParamTypes(Number, Number)
@ReturnType(Number)
add(x, y) {
return x + y;
}
}
// Metadata introspection
let obj = new C("a", 1);
let paramTypes = Reflect.getMetadata("design:paramtypes", obj, "add"); // [Number, Number]
When the abstract operation GetOrCreateMetadataMap is called with Object O, property key P, and Boolean Create the following steps are taken:
"get"
, P)."set"
, P, metadataMap).All ordinary objects have an internal slot called [[Metadata]]. The value of this internal slot is either
When the [[HasMetadata]] internal method of O is called with
When the abstract operation OrdinaryHasMetadata is called with
When the [[HasOwnMetadata]] internal method of O is called with
When the abstract operation OrdinaryHasOwnMetadata is called with
"has"
, MetadataKey)).When the [[GetMatadata]] internal method of O is called with
When the abstract operation OrdinaryGetMetadata is called with
When the [[GetOwnMetadata]] internal method of O is called with
When the abstract operation OrdinaryGetOwnMetadata is called with
"get"
, MetadataKey).When the [[DefineOwnMetadata]] internal method of O is called with
When the abstract operation
"set"
, MetadataKey, MetadataValue).When the [[MetadataKeys]] internal method of O is called with property key P the following steps are taken:
When the abstract operation OrdinaryMetadataKeys is called with Object O and property key P the following steps are taken:
"has"
, key)."has"
, key)."add"
, key)."length"
, k).When the [[OwnMetadataKeys]] internal method of O is called with property key P the following steps are taken:
When the abstract operation OrdinaryOwnMetadataKeys is called with Object O and property key P the following steps are taken:
"keys"
).When the [[DeleteMetadata]] internal method of O is called with
"delete"
, MetadataKey).This section contains amendments to the Reflect object.
A metadata decorator function is an anonymous built-in function that has [[MetadataKey]] and [[MetadataValue]] internal slots.
When a metadata decorator function F is called with arguments target and key, the following steps are taken:
When the metadata
function is called with arguments metadataKey and metadataValue, the following steps are taken:
When the defineMetadata
function is called with arguments metadataKey, metadataValue, target, and propertyKey, the following steps are taken:
When the hasMetadata
function is called with arguments metadataKey, target, and propertyKey, the following steps are taken:
When the hasOwnMetadata
function is called with arguments metadataKey, target, and propertyKey, the following steps are taken:
When the getMetadata
function is called with arguments metadataKey, target, and propertyKey, the following steps are taken:
When the getOwnMetadata
function is called with arguments metadataKey, target, and propertyKey, the following steps are taken:
When the getMetadataKeys
function is called with arguments target and propertyKey, the following steps are taken:
When the getOwnMetadataKeys
function is called with arguments target and propertyKey, the following steps are taken:
When the deleteMetadata
function is called with arguments metadataKey, target, and propertyKey, the following steps are taken: