Is it possible to do something similar to this?
common.js:
function miFuncion() {
console.log(this.functionName); // Imprime por consola miFuncion o miFuncion()
}
Obviously this.functionName
it does not exist or work, it only serves for the example.
You can try this way.
Using the arguments object you can get the callee attribute .
The arguments object contains a reference to the function that is currently being called.
More information on Mozilla MDN
caller
is obsolete, it would be best to usecallee()
with callee()
more natively
One way to obtain the name of the function that is being called, as perhaps some response already indicates, would be using the arguments object, in which the callee attribute is found , which has a property called name , where is the name of the function that has been called.
Another way, even simpler, although I don't know if it is standard or obsolete or not, is to access the name property of a function, like this:
Well, in addition to the example that I wrote, there is one more example that we could include and that is to obtain the calling function, this would be done with the following line:
This one has the name caller (I suppose because of the English of the verb to call) and it allows us to obtain the name of the function that calls our function, in the following way.
I hope that it will serve you or complement a little the already existing information.
To complete the answers, I would like to add one more property about the name of the functions.
Browsers that implement the standard
ES2015
can infer the name of anonymous functions from their syntactic position:The above snippet doesn't work since we haven't given it the option to use
Babel/ES2015
. Let's try it:Also the property
name
is readonly .If we are using the function to instantiate objects and we want to get the name of the class. We can access the property
constructor
:Source: MDN
Example of how to know the name of the class that is being executed, using Object Oriented Programming