0

What is the best way to get an object's name in all browsers OR how to get an object's type name in IE ?

enter image description here

Relevant posts this and this.

In this case the Matrix object is defined in an external package. I just need the name of the type - i'm not interested in a thousand instanceof's ...

EDIT

I'll be more specific about my problem. This is Matrix declaration from math.js. I've got a result object and i want to check if it's a Matrix. Math.js has some types of it's own (like Matrix, Unit) but it also uses common types like String, Number and Array. I'm trying to get the result type as string and i'm trying to avoid checking each type specifically.

Community
  • 1
  • 1
haki
  • 9,389
  • 15
  • 62
  • 110
  • The easiest way that will always work is to define a type field at prototype level that spells the type. – Bart Feb 28 '14 at 19:30
  • `result.value || result.value.constructor`? – Justin Wood Feb 28 '14 at 19:33
  • `typeof` returns a string representation for type. For string it returns "string" for objects "object" so on and so forth....can you elaborate what you mean by 'name' of type? – Vikram Feb 28 '14 at 19:42
  • @Bart - it's an external package and i'm trying to avoid intrusive solutions. – haki Feb 28 '14 at 19:43
  • What specifically about the answer given at http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript/332429#332429 doesn't answer your question here? – Andy E Feb 28 '14 at 19:44
  • @Vikram - as you said - `Typeof` will return `object` and i'm looking for `Matrix` here. – haki Feb 28 '14 at 19:44
  • @Andy E - It's working as expected in Firefox and Chroome - but not in IE. – haki Feb 28 '14 at 19:45
  • 1
    Perhaps if you describe what you're actually trying to accomplish, we might have a better idea exactly what solution would help you. There are a zillion ways to look at the "type" of an object so without knowing what you're actually trying to do, we can't really advise which way to go. – jfriend00 Feb 28 '14 at 19:50
  • @jfriend00 Very good point – Bart Feb 28 '14 at 19:52
  • @haki: there's a solution provided for IE in that answer. – Andy E Feb 28 '14 at 19:53
  • I've edited the original post with more specific details. Thanks. – haki Feb 28 '14 at 19:57
  • Yo heads up `Matrix` isn't supposed to be exposed by `math.js` so you're going behind theyre back by doing this. If you use their minified code or try using a different version of the project this may break. You really should never rely on constructor names – megawac Feb 28 '14 at 20:08

2 Answers2

1

What is the best way to get an object's name

Objects don't have names.

result.value.constructor
 > function Matrix(data) {

That means Matrix is a function, which do have names.

However, using using function.name to get it is a non-standard behavior which shouldn't be relied on.

As you have discovered, it only works in some implementations.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • IE = Headache. Any creative corresponding solutions for IE ? – haki Feb 28 '14 at 19:48
  • @haki AFAIK there isn't a good solution. But you could try converting the function to an string, and parsing its name. – Oriol Feb 28 '14 at 19:50
1

Using the name of the constructor is not reliable: not supported by all browsers, and the name may be mangled when a library is minified or obfuscated.

There are two ways to reliably determine the type of objects:

  1. Using instanceof, i.e.

    if (result instanceof Matrix) console.log('Result is a matrix');
    
  2. All object prototypes you are interested in should be given a special property like _type describing their type, so you can just check

    if (result._type === 'Matrix') console.log('Result is a matrix');
    

In the case of math.js, you can just use the function typeof to get the type of any primitive or math.js specific type. Returned types are lower case.

console.log(math.typeof(result));   // 'matrix'
console.log(math.typeof(1.23));     // 'number'
console.log(math.typeof('hello'));  // 'string'
Jos de Jong
  • 6,602
  • 3
  • 38
  • 58