reflect-metadata
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The reflect-metadata
is a popular npm package that serves as a polyfill for the Metadata Reflection API. This tool is designed to augment classes and their members during the definition phase using a declarative syntax. Its primary function is to allow developers to add additional, consistent metadata to a class. This metadata can then be accessed not just on the object itself, but also through a Proxy, further enhancing its flexibility and usability.
To use reflect-metadata
, you first need to install it using npm with the command npm install reflect-metadata
. Once installed, you can define metadata declaratively on a class using the @Reflect.metadata(metadataKey, metadataValue)
syntax as shown below:
class C {
@Reflect.metadata('metadataKey', 'metadataValue')
method() {
}
}
If you prefer an imperative approach, you can use Reflect.defineMetadata(metadataKey, metadataValue, C.prototype, "method");
as follows:
Reflect.defineMetadata('metadataKey', 'metadataValue', C.prototype, 'method');
To introspect the metadata, the following syntax is used:
let obj = new C();
let metadataValue = Reflect.getMetadata('metadataKey', obj, 'method');
Besides defining and retrieving metadata, the API also allows you to check for the presence of a metadata key and delete metadata from an object or property.
As a JavaScript and SEO expert, I found that the detailed documentation for reflect-metadata
can be found here. This documentation goes over the detailed proposal for the API, it's background, goals, syntax and semantics, API usage, and also discusses potential issues and alternatives. It's an excellent resource for both beginners and advanced users who want to leverage the full potential of the reflect-metadata
npm package.