Is it possible to get the name of the function from inside the function?
Example:
function myFunction() {
// console.log the name of this function without knowing/specifying what the name is
// otherwise console.log('myFunction was called'); is
// more obvious (and better supported) than console.log(myFunction.name);
// console.log(arguments.callee.name); works but not in strict mode and is deprecated
}
Reference:
Function.name
arguments.callee
Update:
From the comments and answers, it appears that the point has been completely missed.
I revised the example to make it clearer.
Update 2:
It seems there is still ambiguity ... here is an example of a debugging process:
function one() {
console.log(arguments.callee.name + ' was called'); // one was called
// reset of the code
}
function two() {
console.log(arguments.callee.name + ' was called'); // two was called
// reset of the code
}
function three() {
console.log(arguments.callee.name + ' was called'); // three was called
// reset of the code
}
function four() {
console.log(arguments.callee.name + ' was called'); // four was called
// reset of the code
}
function five() {
console.log(arguments.callee.name + ' was called'); // five was called
// reset of the code
}
arguments.callee.name
is deprecated and not available in strict mode.
I am looking for a method that is not deprecated and works in strict mode.
Try this...
console.log(myFunction.name);
Be aware that this is not supported by IE yet as mentioned here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.