0
可能重複:
Determine calling function in javascript有沒有什麼方法可以確定哪個函數在javascript中被稱爲myFunction?
假設我有一個JavaScript函數,我想知道誰是主叫方。這可能沒有使用全局變量/參數的方法?
可能重複:
Determine calling function in javascript有沒有什麼方法可以確定哪個函數在javascript中被稱爲myFunction?
假設我有一個JavaScript函數,我想知道誰是主叫方。這可能沒有使用全局變量/參數的方法?
要與各種各樣的瀏覽器兼容,你需要的東西,如:
if (typeof arguments.caller == 'function') {
return arguments.caller;
} else if (typeof arguments.callee == 'function' &&
typeof arguments.callee.caller == 'function') {
return arguments.callee.caller;
} else {
// called from global scope, in strict mode or browser doesn't support
// either of the above access methods
}
注意,在訪問arguments.callee的或主叫方將扔在ES5嚴格模式例外,上述可避免,但我現在無法測試它。