Nodejs get name of current function
In Node.js, I'm trying to obtain the name of current function being executed: Show
This works well, but I'd like (if possible) to obtain the name within the current context. This way, if I renamed the method later I don't have change it manually. Is there a generic object (i.e. similar to 'this') that points to the current function being executed/current context? Thank you. asked Aug 9, 2013 at 18:14
titusmagnustitusmagnus 1,8932 gold badges21 silver badges22 bronze badges 7 Short answer: Long Answer for the 1st option:
answered Dec 26, 2019 at 18:52
3 I don't want to repeat the answers in the "possible duplicate" suggestions from Ian, but there is a solution that might be worth mentioning in addition to them: You could use a named function expression, to have one name, that is accessible from outside of the function and one name that is accessible from inside:
Now, if you decide to give your function a different name, you can just change answered Aug 9, 2013 at 18:28
basilikumbasilikum 10.1k4 gold badges42 silver badges55 bronze badges 12 Not the answer you're looking for? Browse other questions tagged javascript node.js or ask your own question.A Note: In non-standard, pre-ES2015 implementations the The function's The To change it, use
The Function declarationThe
Default-exported function declarationAn
Function constructorFunctions created with the
Function expressionIf the function expression is named, that name is used as the
Anonymous function expressions created using the keyword
However, such cases are rare — usually, in order to refer to the expression elsewhere, the function expression is attached to an identifier when it's created (such as in a variable declaration). In such cases, the name can be inferred, as the following few subsections demonstrate. One practical case where the name cannot be inferred is a function returned from another function:
Variable declaration and methodVariables and methods can infer the name of an anonymous function from its syntactic position.
The same applies to assignment:
Initializer and default valueFunctions in initializers (default values) of destructuring,
default parameters, class fields, etc., will inherit the name of the bound identifier as their
Shorthand method
Bound function
Getter and setterWhen using
ClassA class's name follows the same algorithm as function declarations and expressions.
Warning: JavaScript will set the function's Symbol as function nameIf a
Private propertyPrivate fields and private methods have the hash (
ExamplesTelling the constructor name of an objectYou can use
However, because static members will become own properties of the class, we can't obtain the class name for virtually any class with a
static method property
With a
Due to the existence of static fields,
If a class has a static property called
Therefore you may not rely on the built-in JavaScript compressors and minifiersWarning: Be careful when using the Source code such as:
may be compressed to:
In the uncompressed version, the program runs into the truthy branch and logs "'foo' is an instance of 'Foo'" — whereas, in the compressed version it behaves differently, and runs into the else branch. If you rely on the Specifications
Browser compatibilityBCD tables only load in the browser See also
|