0

I would like to know how I can get the name of my custom object in javascript?

var calendarDay = function (date) {
   //someCode
}

var test = new calendarDay(new Date());
console.log(typeof test); //Object

However, if I do this in Chrome:

console.log(test);

chromeconsolescreenshot

Chrome shows the exact name of my customObject. Can I do this in javascript?

Baral
  • 3,103
  • 2
  • 19
  • 28
  • 2
    Why do you need that? – zerkms May 16 '14 at 02:08
  • @adv12, instanceof returns true or false no? – Baral May 16 '14 at 02:10
  • @zerkms, I want to make sure afunction receives the right "type" of object as args. – Baral May 16 '14 at 02:12
  • 2
    `test instanceof calendarDay` --- that's how you do that, no need to get the name explicitly. – zerkms May 16 '14 at 02:14
  • @zerkms, I understand I can do this. Ty. However, it does not answer my question. Can I achieve what I asked for? – Baral May 16 '14 at 02:16
  • The trivial answer is because javascript is designed so that objects *don't* have names. There are variables and properties that do have names, and they can reference objects. And more than one variable and property can reference the same object, or different objects at different times. In that context, what is an object's "name"? – RobG May 16 '14 at 02:47

2 Answers2

0

Not sure if this could help you. give a name for your custom function obj.

var calendarDay = function (date) {
    this.name = 'calendarDay';
}

var test = new calendarDay(new Date());
console.log(test.name);
Puffy
  • 30
  • 6
0

You're looking for test.constructor.name. You should note however that this will only work with named functions, not function expressions wherein a variable is assigned an anonymous function.

// named function, not function expression
function calendarDay (date) {
   //someCode
}

var test = new calendarDay(new Date());
console.log(test.constructor.name); //"calendarDay"

EDIT:
Big surprise, as @Bergi pointed out, it's not fully supported by IE. Check out the full compatibility table on the MDN Docs

wvandaal
  • 4,265
  • 2
  • 16
  • 27
  • You should mention that it is non-standard and will not work in at least one major browser. – Bergi May 16 '14 at 02:10
  • Hey I tried it but not working?http://jsfiddle.net/jarnanchen/tp356/ – Johnny Chen May 16 '14 at 02:13
  • It won't work at all with the OP's anonymous function – Paul May 16 '14 at 02:14
  • Not working...c.constructor.name.length == 0; – Baral May 16 '14 at 02:14
  • @JrChen it only works with named functions, not function expressions like `var calendarDay = function() {}` – wvandaal May 16 '14 at 02:15
  • @Baral, you need to rename your function so that is a named function expression, otherwise this will not be possible – wvandaal May 16 '14 at 02:18
  • @wvandaal, I renamed one of the millions of function in the app and it worked fine. Thank you for telling me why it is not possible instead of telling me what I should not do without any explanation :-) – Baral May 16 '14 at 02:21