[英]Is it possible to get parent function name in javascript?
class B{
constructor()
// how to determine the function name in which this object was created?
}
}
function a(){
let b = new B();
}
在 BI 的構造函數中需要找出在哪個 function 中調用了它。 我想要 function 名稱,這是a
定示例中的 a 。
這可以用js嗎?
創建一個錯誤並使用它的堆棧;
function foo() {
var err = new Error();
var stack = err.stack; // This is an array of stack frames
}
是的,Error.stack...
class B { constructor() { // how to determine the function name in which this object was created? const ex = new Error() console.log('call from [', ex.stack.split('\n')[2].trim().split(' ')[1], ']'); } } function a() { let b = new B(); } function helloWorld() { let b = new B(); } a(); helloWorld();
是的,使用arguments.callee.caller.name
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.