簡體   English   中英

是否可以在 javascript 中獲得父 function 名稱?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM