[英]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();
}
In the constructor of BI need to find out in which function it was called.在 BI 的构造函数中需要找出在哪个 function 中调用了它。 I want the function name, which is a
in the given example.我想要 function 名称,这是a
定示例中的 a 。
Is this possible with js?这可以用js吗?
create an Error and use its stack;创建一个错误并使用它的堆栈;
function foo() {
var err = new Error();
var stack = err.stack; // This is an array of stack frames
}
Yes, Error.stack...是的,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();
Yes, with arguments.callee.caller.name
.是的,使用arguments.callee.caller.name
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.