自定义装饰器


function require(target, key: string, descriptor) {
    let originMethod = descriptor.value;
    let len = originMethod.length;
    descriptor.value = function (...args) {
        for(let i = 0; i < len; i++) {
            if(undefined == args[i]) {
                throw new Error(`必须传入${args.length}个参数`);
            }
        }
        originMethod.apply(this, args);
    }
}


function trim(target, key: string, descriptor) {
    let originMethod = descriptor.value;
    descriptor.value = function (...args) {
        args.forEach((arg, idx) => {
            if(typeof arg == 'string') {
                args[idx] = arg.trim();
            }
        });
        originMethod.apply(this, args);
    }
}

猜你喜欢

转载自www.cnblogs.com/tujw/p/12411019.html