Reputation: 601
Can some one explain me what can be use case of function's name property in Javascript?, I read that it can be helpful in recursion, how ?
function foo() {}
foo.name; // "foo"
Upvotes: 1
Views: 72
Reputation: 5179
a use case from MDN
You can use obj.constructor.name
to check the "class" of an object
function a(){
}
var b = new a();
alert(b.constructor.name); //Alerts "a"
Think of it the as using get_class()
in PHP or .getClass().getName()
in Java.
Upvotes: 3
Reputation: 4743
I can't think of much of a use case, as it's read only. But it is described on MDN: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/name
I can only imagine you might want to use it in eval
type situations.
Upvotes: 1