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.

12
  • The easiest way that will always work is to define a type field at prototype level that spells the type. Commented Feb 28, 2014 at 19:30
  • result.value || result.value.constructor? Commented Feb 28, 2014 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? Commented Feb 28, 2014 at 19:42
  • @Bart - it's an external package and i'm trying to avoid intrusive solutions. Commented Feb 28, 2014 at 19:43
  • 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. Commented Feb 28, 2014 at 19:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

IE = Headache. Any creative corresponding solutions for IE ?
@haki AFAIK there isn't a good solution. But you could try converting the function to an string, and parsing its name.
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.