Hướng dẫn get function name javascript
Does anyone know if there is a way to get JavaScript function name. For example I got a function like
I have it in my head section. Then I create an object obj1 and put my function there
When I call a method in obj1 object, do I have any way to get my function name (test1) inside of this
method, except parsing the source (
informatik01 15.8k10 gold badges73 silver badges102 bronze badges asked Jul 5, 2010 at 10:52
1
outputs "test" (in Chrome, Firefox and probably Safari). However, If you want to get name from outside you may parse it out of:
but I think name property of function object might be what you need:
this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.
The Red Pea 15.5k16 gold badges91 silver badges120 bronze badges answered Jul 5, 2010 at 10:58
Kamil SzotKamil Szot 16.7k6 gold badges56 silver badges64 bronze badges 11 Until ES2015, there was no standard way to get the
name of a function. Most current browsers support a answered Jul 5, 2010 at 11:38
Tim DownTim Down 309k72 gold badges445 silver badges521 bronze badges 9 The best thing to do is:
Note: Using answered Feb 19, 2015 at 11:58
Vlad A. IonescuVlad A. Ionescu 2,5201 gold badge15 silver badges19 bronze badges 1 Here's what I use to put class names in error messages. It includes code to get the name of functions, which works in most browsers. Obviously, there is no standard way that always works, so you should always provide a name that can be used if no other name is found.
answered Sep 23, 2013 at 21:36
kybernetikoskybernetikos 8,0411 gold badge45 silver badges54 bronze badges 2 This is probably the best way to do it:
This can be done outside the execution of the function, and you can check within the context of the browser console. Happy coding! answered Nov 20, 2014 at 16:29
1 One interesting way I'm experimenting with is a declaration like the following:
It's a little hacky, but what ends up happening is Here's what happens when you use code based on it:
So if you do the following:
You'll then be able to reference answered Oct 7, 2012 at 2:13
jdotjdotjdotjdot 15.4k12 gold badges63 silver badges115 bronze badges You could convert your function into a string
answered Jun 14, 2014 at 1:12
yckartyckart 30.8k9 gold badges117 silver badges127 bronze badges 1 Not the answer you're looking for? Browse other questions tagged javascript or ask your own question. |