init
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
function sum(a: number, b: number): number {
|
||||
return 0;
|
||||
}
|
||||
interface SumFunction extends Function {
|
||||
(a: number, b: number): number;
|
||||
}
|
||||
const mySum = (a: number, b?: number, c?: number): number => {
|
||||
return a + b + c;
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
type DecoratorFunc = (target: any, key: string, descriptor: PropertyDescriptor) => void;
|
||||
|
||||
const createDecorator = (decorator: DecoratorFunc) => (Model: any, key: string) => {
|
||||
const target = Model.prototype;
|
||||
const descriptor = Object.getOwnPropertyDescriptor(target, key);
|
||||
console.log(descriptor);
|
||||
decorator(target, key, descriptor);
|
||||
};
|
||||
const logger: DecoratorFunc = (target, key, descriptor) => {
|
||||
Object.defineProperty(target, key, {
|
||||
...descriptor,
|
||||
value: async (...args: any[]) => {
|
||||
try {
|
||||
console.log(descriptor, this, args);
|
||||
return descriptor.value.apply(this, args);
|
||||
} finally {
|
||||
const now = new Date().getTime();
|
||||
console.log(`lasted logged in ${now.toString()}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
class User {
|
||||
async login() {
|
||||
console.log('login start');
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 10000);
|
||||
console.log('login ................');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const exp1 = () => {
|
||||
console.log('exp1');
|
||||
const loggerDecorator = createDecorator(logger);
|
||||
loggerDecorator(User, 'login');
|
||||
const user = new User();
|
||||
user.login();
|
||||
console.log('user logged', user);
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
const HelloDerorator = <T extends new (...args: any[]) => any>(constructor: T) => {
|
||||
return class extends constructor {
|
||||
newProperty = 'new property';
|
||||
hello = 'override';
|
||||
sayHello() {
|
||||
return this.hello;
|
||||
}
|
||||
};
|
||||
};
|
||||
@HelloDerorator
|
||||
export class Hello {
|
||||
[key: string]: any;
|
||||
hello: string;
|
||||
constructor() {
|
||||
this.hello = 'hello';
|
||||
}
|
||||
}
|
||||
|
||||
export const exp2 = () => {
|
||||
const hello = new Hello();
|
||||
console.log(hello.sayHello());
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
const SetNameDecorator = (firstName: string, secondName: string) => {
|
||||
const name = `${firstName} ${secondName}`;
|
||||
return <T extends new (...args: any[]) => any>(target: T) => {
|
||||
return class extends target {
|
||||
_name: string = name;
|
||||
getMyname() {
|
||||
return this._name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SetNameDecorator('zeng', 'leon')
|
||||
class UserService {
|
||||
[key: string]: any;
|
||||
c() { }
|
||||
}
|
||||
|
||||
export const exp3 = () => {
|
||||
const userService = new UserService();
|
||||
console.log(userService.getMyname());
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
type UserProfile = Record<string, any> & {
|
||||
phone?: number;
|
||||
address?: string;
|
||||
}
|
||||
|
||||
const ProfileDecorator = (profile: UserProfile) => (target: any) => {
|
||||
const Original = target;
|
||||
let userinfo = '';
|
||||
Object.keys(profile).forEach(key => {
|
||||
userinfo += `${key}: ${profile[key]}\n`;
|
||||
})
|
||||
|
||||
Original.prototype.userinfo = userinfo;
|
||||
function constructor(...args: any[]) {
|
||||
console.log('construct has been called');
|
||||
return new Original(...args);
|
||||
}
|
||||
constructor.prototype = Original.prototype;
|
||||
constructor.myinfo = `myinfo ${userinfo}`
|
||||
return constructor as typeof Original;
|
||||
}
|
||||
|
||||
@ProfileDecorator({
|
||||
phone: 1234567890,
|
||||
address: 'zhongguo'
|
||||
})
|
||||
class User { }
|
||||
|
||||
export const exp4 = () => {
|
||||
console.log("------------exp4-----------")
|
||||
const user = new User();
|
||||
console.log((user as any).userinfo);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
|
||||
|
||||
const RoleDecorator = (roles: string[]) => (target: any, key: string) => {
|
||||
if (!target.userRoles) {
|
||||
target.userRoles = [];
|
||||
}
|
||||
console.log("测试编译", key, roles);
|
||||
roles.forEach((role: string) => target.userRoles.push(role));
|
||||
|
||||
}
|
||||
|
||||
const SetRoleDecorator = <T extends new (...args: any[]) => any>(constructor: T) => {
|
||||
const roles = [
|
||||
{ name: 'super-admin', desc: '超级管理员' },
|
||||
{ name: 'admin', desc: '管理员' },
|
||||
{ name: 'user', desc: '普通用户' },
|
||||
]
|
||||
return class extends constructor {
|
||||
constructor(...args: any[]) {
|
||||
super(...args);
|
||||
this.roles = roles.filter((role) => this.userRoles.includes(role.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SetRoleDecorator
|
||||
class UserEntity {
|
||||
@RoleDecorator(['admin', 'user'])
|
||||
roles: string[] = [];
|
||||
}
|
||||
|
||||
export const exp5 = () => {
|
||||
const user = new UserEntity();
|
||||
console.log(user.roles);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
const loggerDecorator = () => {
|
||||
return function logMethod(target: any, propertyName: string, propertyDescriptor: PropertyDescriptor) {
|
||||
const method = propertyDescriptor.value;
|
||||
|
||||
propertyDescriptor.value = async function (...args: any[]) {
|
||||
try {
|
||||
return method.call(target, ...args);
|
||||
} finally {
|
||||
const now = Date.now();
|
||||
console.log(`lasted logged in ${now.toString()}`);
|
||||
}
|
||||
}
|
||||
return propertyDescriptor
|
||||
}
|
||||
}
|
||||
class UserService {
|
||||
@loggerDecorator()
|
||||
async login() {
|
||||
console.log('login success');
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 100);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const exp6 = () => {
|
||||
|
||||
const user = new UserService();
|
||||
user.login();
|
||||
};
|
||||
@@ -0,0 +1,57 @@
|
||||
export const exp7 = () => {
|
||||
|
||||
const userService = new UserService();
|
||||
userService.delete(1);
|
||||
console.log(userService.getUsers());
|
||||
|
||||
};
|
||||
|
||||
type NewType = {
|
||||
id: number;
|
||||
username: string;
|
||||
};
|
||||
const parseConf: ((...args: any[]) => any)[] = [];
|
||||
|
||||
export const parse =
|
||||
(parseTo: (...args: any[]) => any) =>
|
||||
(target: any, propertyName: string, index: number) => {
|
||||
parseConf[index] = parseTo;
|
||||
};
|
||||
|
||||
|
||||
class UserService {
|
||||
private users: NewType[] = [
|
||||
{ id: 1, username: 'admin' },
|
||||
{ id: 2, username: 'pincman' },
|
||||
];
|
||||
|
||||
getUsers() {
|
||||
return this.users;
|
||||
}
|
||||
|
||||
@parseDecorator
|
||||
delete(@parse((arg: any) => Number(arg)) id: number) {
|
||||
this.users = this.users.filter((userObj) => userObj.id !== id);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
// 在函数调用前执行格式化操作
|
||||
export const parseDecorator = (
|
||||
target: any,
|
||||
propertyName: string,
|
||||
descriptor: PropertyDescriptor,
|
||||
): PropertyDescriptor => {
|
||||
console.log('开始格式化数据');
|
||||
return {
|
||||
...descriptor,
|
||||
value(...args: any[]) {
|
||||
// 获取格式化后的参数列表
|
||||
const newArgs = args.map((v, i) =>
|
||||
parseConf[i] ? parseConf[i](v) : v,
|
||||
);
|
||||
console.log('格式化完毕');
|
||||
return descriptor.value.apply(this, newArgs);
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
export const HiddenDecorator = () => {
|
||||
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
|
||||
console.log(descriptor);
|
||||
descriptor.enumerable = false;
|
||||
}
|
||||
}
|
||||
|
||||
export class UserEntity {
|
||||
private _nickname: string;
|
||||
// @ts-ignore
|
||||
private fullName: string;
|
||||
|
||||
@HiddenDecorator()
|
||||
@PrefixDecorator('jesse')
|
||||
get nickname(): string {
|
||||
return this._nickname;
|
||||
}
|
||||
|
||||
set nickname(value: string) {
|
||||
this._nickname = value;
|
||||
this.fullName = `${value}-fullname`;
|
||||
}
|
||||
}
|
||||
|
||||
export const exp8 = () => {
|
||||
const user = new UserEntity();
|
||||
user.nickname = 'leon';
|
||||
console.log(user.nickname);
|
||||
console.log(Object.keys(user));
|
||||
}
|
||||
|
||||
function PrefixDecorator(prefix: string): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor {
|
||||
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
|
||||
return {
|
||||
...descriptor,
|
||||
set(v) {
|
||||
descriptor.set.apply(this, [`${prefix}_${v}`])
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user