zhaoyu_ma 5d3259f632 代码混淆插件. | 8 місяців тому | |
---|---|---|
.. | ||
docs | 8 місяців тому | |
AUTHORS.md | 8 місяців тому | |
CopyrightNotice.txt | 8 місяців тому | |
LICENSE | 8 місяців тому | |
README.md | 8 місяців тому | |
Reflect.d.ts | 8 місяців тому | |
Reflect.js | 8 місяців тому | |
index.d.ts | 8 місяців тому | |
package.json | 8 місяців тому | |
reflect-metadata.d.ts | 8 місяців тому | |
standalone.d.ts | 8 місяців тому | |
typings.d.ts | 8 місяців тому |
npm install reflect-metadata
Declarative definition of metadata:
class C {
@Reflect.metadata(metadataKey, metadataValue)
method() {
}
}
JavaScript
Reflect.defineMetadata(metadataKey, metadataValue, C.prototype, "method");
Imperative introspection of metadata:
let obj = new C();
let metadataValue = Reflect.getMetadata(metadataKey, obj, "method");
// 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() {
}
}
Use properties rather than a separate API.
Obvious downside is that this can be a lot of code:
function ParamTypes(...types) {
return (target, propertyKey) => {
const symParamTypes = Symbol.for("design:paramtypes");
if (propertyKey === undefined) {
target[symParamTypes] = types;
}
else {
const symProperties = Symbol.for("design:properties");
let properties, property;
if (Object.prototype.hasOwnProperty.call(target, symProperties)) {
properties = target[symProperties];
}
else {
properties = target[symProperties] = {};
}
if (Object.prototype.hasOwnProperty.call(properties, propertyKey)) {
property = properties[propertyKey];
}
else {
property = properties[propertyKey] = {};
}
property[symParamTypes] = types;
}
};
}
function ParamTypes(...types) {
// as propertyKey is effectively optional, its easier to use here
return (target, propertyKey) => { Reflect.defineMetadata("design:paramtypes", types, target, propertyKey); }
// vs. having multiple overloads with the target and key in the front:
//
// return (target, propertyKey) => {
// if (propertyKey === undefined) {
// Reflect.defineMetadata(target, "design:paramtypes", types);
// }
// else {
// Reflect.defineMetadata(target, propertyKey, "design:paramtypes", types);
// }
// }
//
// vs. having a different methods for the class or a property:
//
// return (target, propertyKey) => {
// if (propertyKey === undefined) {
// Reflect.defineMetadata(target, "design:paramtypes", types);
// }
// else {
// Reflect.definePropertyMetadata(target, propertyKey, "design:paramtypes", types);
// }
// }
}
To enable experimental support for metadata decorators in your TypeScript project, you must add "experimentalDecorators": true
to your tsconfig.json file.
To enable experimental support for auto-generated type metadata in your TypeScript project, you must add "emitDecoratorMetadata": true
to your tsconfig.json file.