Skip to content

Instantly share code, notes, and snippets.

@rmlzy
Created August 5, 2021 08:17
Show Gist options
  • Save rmlzy/5701a3fa7d0ab1a67aa63226dea69e08 to your computer and use it in GitHub Desktop.
Save rmlzy/5701a3fa7d0ab1a67aa63226dea69e08 to your computer and use it in GitHub Desktop.
Fake reflect-metadata
const Metadata = new WeakMap();
function defineMetadata(key, value, target) {
if (!Metadata.get(target)) {
Metadata.set(target, new Map());
}
const map = Metadata.get(target);
map.set(key, value);
}
function getMetadata(key, target) {
const map = Metadata.get(target);
return map ? map.get(key) : '';
}
Object.defineProperty(Reflect, 'defineMetadata', {
configurable: true,
writable: true,
value: defineMetadata,
});
Object.defineProperty(Reflect, 'getMetadata', {
configurable: true,
writable: true,
value: getMetadata,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment