Class Typescript |每次调用函数时都调用函数
我正在尝试编写一个Typescript API服务。对于该服务,我需要一种方法来检查在调用函数(如Class Typescript |每次调用函数时都调用函数,class,typescript,ecmascript-6,Class,Typescript,Ecmascript 6,我正在尝试编写一个Typescript API服务。对于该服务,我需要一种方法来检查在调用函数(如get)时该方法是否存在 我意识到我可以像你一样 get(endpoint: string) { this.handleRequest(); } post(endpoint: string, data: any) { this.handleRequest(); } 但我并不特别想在每个方法的顶部这样做,所以我不知道是否有办法在Typescript类的构造函数中侦听子函数的调用 能
get
)时该方法是否存在
我意识到我可以像你一样
get(endpoint: string) {
this.handleRequest();
}
post(endpoint: string, data: any) {
this.handleRequest();
}
但我并不特别想在每个方法的顶部这样做,所以我不知道是否有办法在Typescript类的构造函数中侦听子函数的调用
能做到这一点似乎有点牵强,但在像我这样的情况下它将非常有用,所以我不必继续这样做
export class ApiService {
base_url: string = 'https://jsonplaceholder.typicode.com/posts';
methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
/**
* Create an instance of ApiService.
* @param {string} base_url
*/
constructor(base_url: string = null) {
this.base_url = base_url ? base_url : this.base_url;
}
get(endpoint: string): string {
// duplicated line
this.handleRequest();
return 'get method';
}
post(endpoint: string, data: any): string {
// duplicated line
this.handleRequest();
return 'post method';
}
protected handleRequest(): string {
return 'handle the request';
}
}
您可以使用一个decorator来实现这一点,该decorator将使用一个调用原始实现和额外方法的方法覆盖类的所有方法:
function handleRequest() {
return function<TFunction extends Function>(target: TFunction){
for(let prop of Object.getOwnPropertyNames(target.prototype)){
if (prop === 'handleRequest') continue;
// Save the original function
let oldFunc: Function = target.prototype[prop];
if(oldFunc instanceof Function) {
target.prototype[prop] = function(){
this['handleRequest'](); // call the extra method
return oldFunc.apply(this, arguments); // call the original and return any result
}
}
}
}
}
@handleRequest()
export class ApiService {
base_url: string = 'https://jsonplaceholder.typicode.com/posts';
methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
/**
* Create an instance of ApiService.
* @param {string} base_url
*/
constructor(base_url: string = null) {
this.base_url = base_url ? base_url : this.base_url;
}
get(endpoint: string): string {
return 'get method';
}
post(endpoint: string, data: any): string {
return 'post method';
}
protected handleRequest(): void {
console.log('handle the request');
}
}
let s = new ApiService();
s.get("");
s.post("", null);
函数handleRequest(){
返回函数(目标:t函数){
for(让Object.getOwnPropertyNames的prop(target.prototype)){
如果(prop=='HandlerRequest')继续;
//保存原始函数
让oldFunc:Function=target.prototype[prop];
if(oldFunc instanceof函数){
target.prototype[prop]=函数(){
这个['handleRequest']();//调用额外的方法
return oldFunc.apply(this,arguments);//调用原始函数并返回任何结果
}
}
}
}
}
@HandlerRequest()
出口级服务{
基本url:string='1!'https://jsonplaceholder.typicode.com/posts';
方法=['GET','POST','PUT','PATCH','DELETE'];
/**
*创建ApiService的实例。
*@param{string}base_url
*/
构造函数(基本url:string=null){
this.base\u url=base\u url?base\u url:this.base\u url;
}
获取(端点:字符串):字符串{
返回“get方法”;
}
post(端点:字符串,数据:任意):字符串{
返回“post方法”;
}
受保护的HandlerRequest():void{
log(“处理请求”);
}
}
设s=newapiservice();
s、 得到(“”);
s、 post(“,空);
在HandlerRequest中执行该操作
?哪个方法(或函数)存在或不存在?你想怎么检查?你能把所有重复检查的地方都贴上完整的代码吗?你是什么意思?我希望能够调用api.get('endpoint')
然后将返回值,但只要api.get('endpoint')
或api.post('endpoint',['name'=>'tallent'])
运行this.handleRequest()但是我不想重复这个.handleRequest()对于所有的方法,我已经更新了这个问题,以包含完整的代码。装饰器是一个可行的解决方案吗?您仍然必须将装饰器附加到每个方法。