TypeScript之調(diào)用棧的實(shí)現(xiàn)
本文介紹了TypeScript之調(diào)用棧,分享給大家,具體如下:
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)}: \n ${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();
}
}
測(cè)試代碼:
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)}`);
}
}
開始測(cè)試:

結(jié)果:

所以,盡量給function的參數(shù)取一些好的名字.
另外一點(diǎn) , 不會(huì)出現(xiàn)function()這樣的打印 , 出現(xiàn)沒有參數(shù)的function , 我會(huì)將方法體內(nèi)容也打印出來(lái)
如果需要把每一個(gè)function的方法體的內(nèi)容打印出來(lái)CallStackTool.printCallStack(2,false), 將第二個(gè)參數(shù)設(shè)置未false
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- TypeScript棧的壓入與彈出序列校驗(yàn)
- 前端算法之TypeScript包含min函數(shù)的棧實(shí)例詳解
- TypeScript數(shù)組實(shí)現(xiàn)棧與對(duì)象實(shí)現(xiàn)棧的區(qū)別詳解
- Typescript是必須要學(xué)習(xí)嗎?如何學(xué)習(xí)TS全棧開發(fā)
- 數(shù)據(jù)結(jié)構(gòu)TypeScript之棧和隊(duì)列詳解
- TypeScript數(shù)據(jù)結(jié)構(gòu)棧結(jié)構(gòu)Stack教程示例
- Typescript實(shí)現(xiàn)棧的方法示例
相關(guān)文章
JS獲取瀏覽器語(yǔ)言動(dòng)態(tài)加載JS文件示例代碼
通過(guò)獲取瀏覽器語(yǔ)言版本,來(lái)相對(duì)的加載easyui語(yǔ)言包就是動(dòng)態(tài)加載JS文件,下面有個(gè)不錯(cuò)的實(shí)例,大家可以看看2014-10-10
基于javascript數(shù)組實(shí)現(xiàn)圖片輪播
這篇文章主要為大家詳細(xì)介紹了基于javascript數(shù)組實(shí)現(xiàn)圖片輪播的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
JavaScript 無(wú)縫上下左右滾動(dòng)加定高定寬停頓效果(兼容ie/ff)
JavaScript 指定寬度高度的無(wú)間斷滾動(dòng)實(shí)現(xiàn)代碼,這樣的效果適合作為焦點(diǎn)新聞的輪播顯示。2010-03-03
解決layui laydate 時(shí)間控件一閃而過(guò)的問題
今天小編就為大家分享一篇解決layui laydate 時(shí)間控件一閃而過(guò)的問題,具有好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
詳解JavaScript基于面向?qū)ο笾畡?chuàng)建對(duì)象(1)
這篇文章主要介紹了JavaScript基于面向?qū)ο笾畡?chuàng)建對(duì)象,對(duì)創(chuàng)建對(duì)象進(jìn)行了詳細(xì)描述,感興趣的小伙伴們可以參考一下2015-12-12
不同編碼的頁(yè)面表單數(shù)據(jù)亂碼問題解決方法
這篇文章主要介紹了不同編碼的頁(yè)面表單數(shù)據(jù)亂碼問題解決方法,本文使用一個(gè)表單不常用屬性accept-charset解決了這個(gè)問題,需要的朋友可以參考下2015-02-02

