You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var__decorate=(this&&this.__decorate)||function(decorators,target,key,desc){varc=arguments.length,r=c<3
? target
: desc===null
? (desc=Object.getOwnPropertyDescriptor(target,key))
: desc,d;console.log('r',r)if(typeofReflect==="object"&&typeofReflect.decorate==="function")r=Reflect.decorate(decorators,target,key,desc);elsefor(vari=decorators.length-1;i>=0;i--)if((d=decorators[i]))r=(c<3 ? d(r) : c>3 ? d(target,key,r) : d(target,key))||r;console.log(c)returnc>3&&r&&Object.defineProperty(target,key,r),r;};var__metadata=(this&&this.__metadata)||function(k,v){if(typeofReflect==="object"&&typeofReflect.metadata==="function")returnReflect.metadata(k,v);};var__param=(this&&this.__param)||function(paramIndex,decorator){returnfunction(target,key){decorator(target,key,paramIndex);}};// You could think of it in three ways://// - A place to learn TypeScript in a place where nothing can break// - A place to experiment with TypeScript syntax, and share the URLs with others// - A sandbox to experiment with different compiler features of TypeScript// To learn more about the language, click above in "Examples" or "What's New".// Otherwise, get started by removing these comments and the world is your playground.// 方法装饰器 - 如果方法装饰器返回一个值,它会被用作方法的属性描述符。functiondecorateMethod(str){returnfunction(target,key,value){return{value: function(...args){console.log('decorateMethod',str);// decorateMethodconsole.log('target',target);// 对于静态成员来说是类的构造函数,对于实例成员是类的原型对象console.log('target === C.prototype',target===C.prototype);// trueconsole.log('key',key);// 方法名console.log('value',value);// 成员的属性描述符 Object.getOwnPropertyDescriptorconsole.log('args',args);vara=args.map(a=>JSON.stringify(a)).join();varresult=value.value.apply(this,args);varr=JSON.stringify(result);console.log(`Call: ${key}(${a}) => ${r}`);returnresult;}};};}// 类装饰器 - 如果类装饰器返回一个值,它会使用提供的构造函数来替换类的声明。functiondecorateClass(str){returnfunction(constructor){console.log(constructor);// 参数为类的构造函数};}// 访问器装饰器 - 如果访问器装饰器返回一个值,它会被用作方法的属性描述符。functionconfigurable(value){returnfunction(target,propertyKey,descriptor){descriptor.configurable=value;};}// 装饰器工厂 - 闭包functionformat(formatString){returnReflect.metadata('formatMetadataKey',formatString);}// 属性装饰器 - 返回值也会被忽略。因此,属性描述符只能用来监视类中是否声明了某个名字的属性functiongetFormat(target,propertyKey){console.log('target');//对于静态成员来说是类的构造函数,对于实例成员是类的原型对象console.log('propertyKey');// 属性名returnReflect.getMetadata('formatMetadataKey',target,propertyKey);}// 参数装饰器 - 参数装饰器的返回值会被忽略。functionrequired(target,propertyKey,parameterIndex){console.log('target');// //对于静态成员来说是类的构造函数,对于实例成员是类的原型对象console.log('propertyKey');// 参数名console.log('parameterIndex');// 参数在函数参数列表中的索引。}letC=classC{constructor(){this.str='string';}sum(x,y){returnx+y;}getx(){returnthis.str;}};__decorate([format("Hello, %s"),__metadata("design:type",String)],C.prototype,"str",void0);__decorate([decorateMethod('decorateMethod'),__param(0,required),__metadata("design:type",Function),__metadata("design:paramtypes",[Number,Number]),__metadata("design:returntype",void0)],C.prototype,"sum",null);__decorate([configurable(false),__metadata("design:type",Object),__metadata("design:paramtypes",[])],C.prototype,"x",null);C=__decorate([decorateClass('decorateClass'),__metadata("design:paramtypes",[])],C);console.log(newC().sum(1,2));// 3
The text was updated successfully, but these errors were encountered:
装饰器种类
方法日志装饰器底层实现 - __decorate()
The text was updated successfully, but these errors were encountered: