Home  >  Article  >  Backend Development  >  javascript - 函数内如何知道调用它的函数的名称?

javascript - 函数内如何知道调用它的函数的名称?

WBOY
WBOYOriginal
2016-06-06 20:37:081005browse

如:

<code>function a(){
    b();
}
function b(){
    console.log(a)
}
</code>

a()不向b()传值的情况下,函数b()如何知道调用它的是a()
js或者php都想知道有没有这种方法

回复内容:

如:

<code>function a(){
    b();
}
function b(){
    console.log(a)
}
</code>

a()不向b()传值的情况下,函数b()如何知道调用它的是a()
js或者php都想知道有没有这种方法

js:arguments.callee.caller

在javascript的函数内,arguments.callee是这个函数本身,而函数的caller则是这个函数的调用者(函数的调用者只有函数调用期间/函数内部才存在)。

PHP的话用debug_backtrace

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn