How does JS know within the function body which function to run inside?
To make an analogy:
function A () { /// I want to know here my function name A and functions a () {...}}
The call stack that uses error can match the function that is executed during the current period:
// Gets the function that is being executed getexecfunction () {new Error (). Stack.match (/at (. * ?) /g); = Names[1].replace (' at ', '). Trim (); return eval (name);}
Use Ex:
function A () { console.log (getexecfunction ());} function getexecfunction () { new Error (). Stack.match (/at (. *)/g); = Names[1].replace (' at ', '). Trim (); return eval (name);} A (); // [Function:a]
If it is inside class, you can write this (code based on ES6):
class A { // gets the function being executed getexecfunction () { new Error (). Stack.match (/at a\. *?) /g); = Names[1].replace (' at A. ', '). Trim (); return eval ('this. ${name} '); A () { Console.log (this. getexecfunction ());} } New A (). a (); // [function:a]
If there is any mistake, please correct me, thank you.
node. js gets the current function within the function