TypeScript 호출 스 택 구현

본 고 는 TypeScript 호출 스 택 을 소개 하고 여러분 에 게 공유 합 니 다.구체 적 으로 다음 과 같 습 니 다.

class CallStackTool{
  private static index:number = 0;
  public static printCallStack (count:number , simple: boolean = true):void {
    let caller:Function = arguments.callee.caller;
    let i:number = 0;
    count = count || 10;
    CallStackTool.index ++;
    if( CallStackTool.index > 500 ) CallStackTool.index = 1;
    console.log(`***-----------------${CallStackTool.index}Start----------------------- **`);
    while (caller && i < count) {
      console.log(`${(i+1)}: 
${CallStackTool.getFunctionName(caller,simple)}`); caller = caller.caller; i++; } console.log(`***-----------------${CallStackTool.index}End----------------------- **`); } private static getFunctionName(func:any,simple: boolean):string { if( simple ){ let name:any; if ( typeof func == 'function' ) { name = ('' + func).match(/function\s*\((\s*\$*\S+\s*,)*(\s*\$*\S+\s*)?\)/g); let $result: string = name && name[0]; if( $result != `function ()` ){ return $result; } } } return func.toString(); } }
테스트 코드:

class Test2CallStack{

  public add( i:number, b:number ):number{
    CallStackTool.printCallStack(2,true);
    return i +b;
  }

  public a( c:number, q:number ): number{
    return this.add(c,q);
  }

  public print() : void{
    console.log(`${this.a(1,1)}`);
  }
}
테스트 시작:

결과:

그래서 가능 한 한 function 의 매개 변수 에 좋 은 이름 을 지어 주세요.
또 하 나 는 function()와 같은 인쇄 가 나타 나 지 않 고 인자 가 없 는 function 이 나타 나 지 않 습 니 다.방법 체 내용 도 인쇄 하 겠 습 니 다.
모든 function 의 방법 체 의 내용 을 출력 해 야 한다 면 CallStackTool.printCallStack(2,false),두 번 째 매개 변 수 를 false 가 아 닌 것 으로 설정 합 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기