nitesh sharma
nitesh sharma

Reputation: 601

Possible use case of a function's name property in Javascript

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

Answers (2)

Adi
Adi

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

Adam Heath
Adam Heath

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

Related Questions