0% found this document useful (0 votes)
40 views

How Do I Get The Name of An Object's Type in JavaScript - Stack Overflow

The document discusses multiple ways to determine an object's type in JavaScript, including using the constructor property, instanceof operator, constructor name property, and Object.prototype.toString method. It provides examples and caveats for each approach, and notes that the best method depends on how the object was constructed and what specifically needs to be determined. Cross-frame compatibility and different construction patterns can impact the reliability of these approaches.

Uploaded by

madscijr
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

How Do I Get The Name of An Object's Type in JavaScript - Stack Overflow

The document discusses multiple ways to determine an object's type in JavaScript, including using the constructor property, instanceof operator, constructor name property, and Object.prototype.toString method. It provides examples and caveats for each approach, and notes that the best method depends on how the object was constructed and what specifically needs to be determined. Cross-frame compatibility and different construction patterns can impact the reliability of these approaches.

Uploaded by

madscijr
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


sign up log in tour help careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Tell me more

How do I get the name of an object's type in JavaScript?

Is there a JavaScript equivalent of Java's c l a s s . g e t N a m e ( )?


javascript

edited Oct 4 '10 at 10:55 Peter Mortensen 6,772 8 45 75 add comment

asked Dec 1 '08 at 22:06 Ewen Cartwright 2,732 3 11 15

13 Answers
Here is a hack that will do what you need - be aware that it modifies the Object's prototype, something people frown upon (usually for good reason) O b j e c t . p r o t o t y p e . g e t N a m e=f u n c t i o n ( ){ v a rf u n c N a m e R e g e x=/ f u n c t i o n( . { 1 , } ) \ ( / ; v a rr e s u l t s=( f u n c N a m e R e g e x ) . e x e c ( ( t h i s ) . c o n s t r u c t o r . t o S t r i n g ( ) ) ; r e t u r n( r e s u l t s& &r e s u l t s . l e n g t h>1 )?r e s u l t s [ 1 ]:" " ; } ; Now, all of your objects will have the function, g e t N a m e ( ), that will return the name of the constructor as a string. I have tested this in FF3 and IE7, I can't speak for other implementations. If you don't want to do that, here is a discussion on the various ways of determining types in JavaScript...

I recently updated this to be a bit more exhaustive, though it is hardly that. Corrections welcome...

Using the c o n s t r u c t o rproperty...


Every object has a value for its c o n s t r u c t o r property, but depending on how that object was constructed as well as what you want to do with that value, it may or may not be useful. Generally speaking, you can use the c o n s t r u c t o r property to test the type of the object like so: v a rm y A r r a y=[ 1 , 2 , 3 ] ; ( m y A r r a y . c o n s t r u c t o r= =A r r a y ) ;/ /t r u e So, that works well enough for most needs. That said...

Caveats
An example where it isn't as obvious is using multiple inheritance: f u n c t i o na ( ){t h i s . f o o=1 ; } f u n c t i o nb ( ){t h i s . b a r=2 ;} b . p r o t o t y p e=n e wa ( ) ;/ /bi n h e r i t sf r o ma Things now don't work as you might expect them to: v a rf=n e wb ( ) ;/ /c r e a t en e wo b j e c tw i t ht h ebc o n s t r u c t o r ( f . c o n s t r u c t o r= =b ) ;/ /f a l s e ( f . c o n s t r u c t o r= =a ) ;/ /t r u e So, you might get unexpected results if the object your testing has a different object set as its prototype. There are ways around this outside the scope of this discussion. There are other uses for the c o n s t r u c t o r property, some of them interesting, others not so much; for now we will not delve into those uses since it isn't relevant to this discussion.

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

1/10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


Will not work cross-frame and cross-window
Using . c o n s t r u c t o r for type checking will break when you want to check the type of objects coming from different w i n d o w objects, say that of an iframe or a popup window. This is because there's a different version of each core type constructor in each `window', i.e. i f r a m e . c o n t e n t W i n d o w . A r r a y= = =A r r a y/ /f a l s e

Using the i n s t a n c e o foperator...


The i n s t a n c e o f operator is a clean way of testing object type as well, but has its own potential issues, just like the c o n s t r u c t o r property. v a rm y A r r a y=[ 1 , 2 , 3 ] ; ( m y A r r a yi n s t a n c e o fA r r a y ) ;/ /t r u e ( m y A r r a yi n s t a n c e o fO b j e c t ) ;/ /t r u e But i n s t a n c e o f fails to work for primitive values 3i n s t a n c e o fN u m b e r/ /f a l s e ' a b c 'i n s t a n c e o fS t r i n g/ /f a l s e t r u ei n s t a n c e o fB o o l e a n/ /f a l s e A wrapper is needed around primitives in order for i n s t a n c e o f to work, for example n e wN u m b e r ( 3 )i n s t a n c e o fN u m b e r/ /t r u e This is ironic because the . c o n s t r u c t o r check works fine for primitives 3 . . c o n s t r u c t o r= = =N u m b e r/ /t r u e ' a b c ' . c o n s t r u c t o r= = =S t r i n g/ /t r u e t r u e . c o n s t r u c t o r= = =B o o l e a n/ /t r u e Why two dots for the 3? Because Javascript interprets the first dot as a decimal point ;)

Will not work cross-frame and cross-window


i n s t a n c e o f also will not work across different windows, for the same reason as the constructor property check.

Using the n a m eproperty of the c o n s t r u c t o rproperty...


Does NOT work in <IE9
Using m y O b j e c t I n s t a n c e . c o n s t r u c t o r . n a m e will give you a string containing the name of the constructor function used, but is subject to the caveats about the constructor property that were mentioned earlier. For IE9 and above, you can monkey-patch in support: i f( F u n c t i o n . p r o t o t y p e . n a m e= = =u n d e f i n e d& &O b j e c t . d e f i n e P r o p e r t y! = =u n d e f i n e d ){ O b j e c t . d e f i n e P r o p e r t y ( F u n c t i o n . p r o t o t y p e ,' n a m e ' ,{ g e t :f u n c t i o n ( ){ v a rf u n c N a m e R e g e x=/ f u n c t i o n \ s + ( . { 1 , } ) \ s * \ ( / ; v a rr e s u l t s=( f u n c N a m e R e g e x ) . e x e c ( ( t h i s ) . t o S t r i n g ( ) ) ; r e t u r n( r e s u l t s& &r e s u l t s . l e n g t h>1 )?r e s u l t s [ 1 ]:" " ; } , s e t :f u n c t i o n ( v a l u e ){ } } ) ; }

Using Object.prototype.toString
It turns out, as this post details, you can use Object.prototype.toString - the low level and generic implementation of toString - to get the type for all built-in types

O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( ' a b c ' )/ /[ o b j e c tS t r i n g ] O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( / a b c / )/ /[ o b j e c tR e g E x p ] O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( [ 1 , 2 , 3 ] )/ /[ o b j e c tA r r a y ] One could write a short helper function such as f u n c t i o nt y p e ( o b j ) { r e t u r nO b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( o b j ) . s l i c e ( 8 ,1 ) ; }

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

to remove the cruft and get at just the type name

2/10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


to remove the cruft and get at just the type name t y p e ( ' a b c ' )/ /S t r i n g However, it will return 'Object' for all user-defined types.

Caveats for all...


All of these are subject to one potential problem, and that is the question of how the object in question was constructed. Here are various ways of building objects and the values that the different methods of type checking will return:

/ /u s i n gan a m e df u n c t i o n : f u n c t i o nF o o ( ){t h i s . a=1 ;} v a ro b j=n e wF o o ( ) ; ( o b ji n s t a n c e o fO b j e c t ) ; ( o b ji n s t a n c e o fF o o ) ; ( o b j . c o n s t r u c t o r= =F o o ) ; ( o b j . c o n s t r u c t o r . n a m e= =" F o o " ) ;

/ /t r u e / /t r u e / /t r u e / /t r u e

/ /l e t ' sa d ds o m ep r o t o t y p i c a li n h e r i t a n c e f u n c t i o nB a r ( ){t h i s . b=2 ;} F o o . p r o t o t y p e=n e wB a r ( ) ; o b j=n e wF o o ( ) ; ( o b ji n s t a n c e o fO b j e c t ) ; / /t r u e ( o b ji n s t a n c e o fF o o ) ; / /t r u e ( o b j . c o n s t r u c t o r= =F o o ) ; / /f a l s e ( o b j . c o n s t r u c t o r . n a m e= =" F o o " ) ; / /f a l s e

/ /u s i n ga na n o n y m o u sf u n c t i o n : o b j=n e w( f u n c t i o n ( ){t h i s . a=1 ;} ) ( ) ; ( o b ji n s t a n c e o fO b j e c t ) ; / /t r u e ( o b j . c o n s t r u c t o r= =o b j . c o n s t r u c t o r ) ;/ /t r u e ( o b j . c o n s t r u c t o r . n a m e= =" " ) ; / /t r u e

/ /u s i n ga na n o n y m o u sf u n c t i o na s s i g n e dt oav a r i a b l e v a rF o o=f u n c t i o n ( ){t h i s . a=1 ;} ; o b j=n e wF o o ( ) ; ( o b ji n s t a n c e o fO b j e c t ) ; / /t r u e ( o b ji n s t a n c e o fF o o ) ; / /t r u e ( o b j . c o n s t r u c t o r= =F o o ) ; / /t r u e ( o b j . c o n s t r u c t o r . n a m e= =" " ) ;/ /t r u e

/ /u s i n go b j e c tl i t e r a ls y n t a x o b j={f o o:1} ; ( o b ji n s t a n c e o fO b j e c t ) ; / /t r u e ( o b j . c o n s t r u c t o r= =O b j e c t ) ; / /t r u e ( o b j . c o n s t r u c t o r . n a m e= =" O b j e c t " ) ;/ /t r u e While not all permutations are present in this set of examples, hopefully there are enough to provide you with an idea about how messy things might get depending on your needs. Don't assume anything, if you don't understand exactly what you are after, you may end up with code breaking where you don't expect it to because of a lack of grokking the subtleties.

NOTE:

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

3/10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


Discussion of the t y p e o f operator may appear to be a glaring omission, but it really isn't useful in helping to identify whether an object is a given type, since it is very simplistic. Understanding where t y p e o f is useful is important, but I don't currently feel that it is terribly relevant to this discussion. My mind is open to change though. :)
edited Jul 17 at 16:07 community wiki 14 revs, 6 users 67% Jason Bunting

88 What a thorough answer! Joshua Carmody Dec 5 '08 at 18:17 19 Well, I figured I might as well - the point of Stack Overflow is to be a bit like a wiki, and this is much more in
line with that intent, I think. Regardless, I just wanted to be somewhat thorough. Jason Bunting Dec 5 '08 at 20:47

It will work if you do it like this function a() { this.a = 1;} function b() { this.b = 2; } b.prototype = new a(); // b inherits from a b.prototype.constructor = b; // Correct way of prototypical inheritance var f = new b(); // create new object with the b constructor (f.constructor == b); // TRUE (f.constructor == a); // FALSE avok00 Jan 10 '11 at 15:36 Now, this is how most of the answers should be on StackOverflow. (don't take length of the answer as a defining parameter, but the comprehensiveness) Kumar Harsh Aug 12 '12 at 19:51 It's important to note that any techniques that inspect the object's c o n s t r u c t o r method (either with . t o S t r i n g ( ) or . n a m e) will fail to work if your Javascript has been minified with a tool like uglify, or the Rails asset pipeline. The minification renames the constructor, so you will end up with incorrect class names like n. If you're in this scenario, you may want to just manually define a c l a s s N a m e property on your objects and use that instead. Gabe Martin-Dempesy Dec 28 '12 at 16:12

2 7

show 11 more comments

DO NOT USE THE CONSTRUCTOR PROPERTY. Read THIS first. The correct code is: f u n c t i o ng e t _ t y p e ( t h i n g ) { i f ( t h i n g = = = n u l l ) r e t u r n" [ o b j e c tN u l l ] " ;/ /s p e c i a lc a s e r e t u r nO b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( t h i n g ) ; } / /e x a m p l er e s u l t s : g e t _ t y p e ( n u l l ) -[ o b j e c tN u l l ] g e t _ t y p e ( w i n d o w ) -[ o b j e c tW i n d o w ] g e t _ t y p e ( [ ] ) -[ o b j e c tA r r a y ] g e t _ t y p e ( [ ' 1 ' ] ) -[ o b j e c tA r r a y ] g e t _ t y p e ( { } ) -[ o b j e c tO b j e c t ] g e t _ t y p e ( d o c u m e n t ) -[ o b j e c tH T M L D o c u m e n t ] g e t _ t y p e ( d o c u m e n t . g e t E l e m e n t B y I d )-[ o b j e c tF u n c t i o n ] NB: According to specs, this function is the most reliable between different browsers.
answered Sep 29 '10 at 21:42 Christian 10.5k 3 35 81

This isn't entirely correct: g e t _ t y p e ( u n d e f i n e d ) ;, g e t _ t y p e ( t r u e ) ;, g e t _ t y p e ( N a N ) ;, g e t _ t y p e ( ' ' ) ; and g e t _ t y p e ( e v a l ) ; imply the argument is an object while t y p e o fu n d e f i n e d ;, t y p e o ft r u e ;, t y p e o fN a N ;, t y p e o f' ' ; and t y p e o fe v a l ; all return something else. In JavaScript, type is a property of value, hence objects don't have a type but a class. The PerfectionKills blog post actually contains a very good implementation by extending the Object prototype with g e t C l a s s. Saul Aug 28 '11 at 15:51 @Saul I've read that blog, and it doesn't contradict what I said. If the OP wants cross-browser results, g e t _ t y p e is the way to go. Christian Aug 28 '11 at 22:41 What makes you think extending O b j e c t . p r o t o t y p e isn't a cross-browser solution? It is supported by virtually every modern browser and even by IE6. Saul Aug 29 '11 at 6:25 @Saul I never said it is not. I just said the constructor is not a cross-browser solution. The (well-deserved) accepted answer above uses a variation of my code as well as the PerfectionKills one. Christian Aug 29 '11 at 8:10 Er.. my initial comment was about extending O b j e c t . p r o t o t y p e not constructor. What I meant was that g e t _ t y p e gives misleading results by implying that all values in JavaScript are of o b j e c t type. Saul Aug 29 '11 at 9:27

show 11 more comments

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

4/10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


Jason Bunting's answer gave me enough of a clue to find what I needed: < < O b j e c ti n s t a n c e > > . c o n s t r u c t o r . n a m e So, for example, in the following piece of code: f u n c t i o nM y O b j e c t ( ){ } v a rm y I n s t a n c e=n e wM y O b j e c t ( ) ; m y I n s t a n c e . c o n s t r u c t o r . n a m e would return " M y O b j e c t ".
edited Dec 1 '08 at 22:35 answered Dec 1 '08 at 22:21 Ewen Cartwright 2,732 3 11 15

For completeness, it might be worth mentioning that using constructor.name only works if you used a named function as the constructor as opposed to an anonymous function assigned to a variable. Matthew Crumley Dec 1 '08 at 22:24 For completeness, it might worth mentioning that it doesn't work in IE browsers --- they do not support the "name" attribute on functions. Eugene Lazutkin Dec 2 '08 at 3:30 @Eugene - I forgot about that... I guess I've spent too much time doing javascript outside browsers. Matthew Crumley Dec 5 '08 at 23:14

add comment

Update
To be precise, I think OP asked for a function that retrieves the constructor name for a particular object. In terms of Javascript, o b j e c t does not have a type but is a type of and in itself. However, different objects can have different constructors. O b j e c t . p r o t o t y p e . g e t C o n s t r u c t o r N a m e=f u n c t i o n( ){ v a rs t r=( t h i s . p r o t o t y p e?t h i s . p r o t o t y p e . c o n s t r u c t o r:t h i s . c o n s t r u c t o r ) . t o S t r i n g ( ) ; v a rc n a m e=s t r . m a t c h ( / f u n c t i o n \ s ( \ w * ) / ) [ 1 ] ; v a ra l i a s e s=[ " " ," a n o n y m o u s " ," A n o n y m o u s " ] ; r e t u r na l i a s e s . i n d e x O f ( c n a m e )>1?" F u n c t i o n ":c n a m e ; } n e wA r r a y ( ) . g e t C o n s t r u c t o r N a m e ( ) ; / /r e t u r n s" A r r a y " ( f u n c t i o n( ){ } ) ( ) . g e t C o n s t r u c t o r N a m e ( ) ;/ /r e t u r n s" F u n c t i o n "

Note: the below example is deprecated. A blog post linked by Christian Sciberras contains a good example on how to do it. Namely, by extending the Object prototype: i f( ! O b j e c t . p r o t o t y p e . g e t C l a s s N a m e ){ O b j e c t . p r o t o t y p e . g e t C l a s s N a m e=f u n c t i o n( ){ r e t u r nO b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( t h i s ) . m a t c h ( / ^ \ [ o b j e c t \ s ( . * ) \ ] $ / ) [ 1 ] ; } } v a rt e s t=[ 1 , 2 , 3 , 4 , 5 ] ; a l e r t ( t e s t . g e t C l a s s N a m e ( ) ) ;/ /r e t u r n sA r r a y

edited Jun 4 '12 at 14:43

answered Aug 28 '11 at 15:59 Saul 9,655 4 25 58

Nice, but we're into naming again: JS doesn't haven't classes. mikemaccana Jun 4 '12 at 13:34 @nailer - I recommend to use the updated function, the older one is kept for merely historic reasons. Saul Jun 4 '12 at 13:51 This works but it should be noted that it could be done without modifying Object.prototype, by creating a function that takes the object as a first argument and uses that instead of 'this' inside the function. Matt Browne Nov 11 '12 at 23:23 @Matt - Sure. It is just that having an object method is more terse: t e s t . g e t C l a s s N a m e ( ) vs g e t C l a s s N a m e . a p p l y ( t e s t ). Saul Nov 12 '12 at 7:54 add comment

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

5/10

12/12/13
A little trick I use:

How do I get the name of an object's type in JavaScript? - Stack Overflow

f u n c t i o nS q u a r e ( ) { t h i s . c l a s s N a m e=" S q u a r e " ; t h i s . c o r n e r s=4 ; } v a rM y S q u a r e=n e wS q u a r e ( ) ; c o n s o l e . l o g ( M y S q u a r e . c l a s s N a m e ) ;/ /" S q u a r e "


edited May 13 at 0:11 answered Jun 16 '11 at 22:25 ajax81 2,838 13 27

4 1

I do not particularly like this. It's more a kind of dirty trick. On the other hand, if you don't have too many constructors, it might work just fine. pimvdb Jun 17 '11 at 7:56 @pimvdb: I think it's cleaner than modifying the object's prototype, a la the accepted answer. ajax81 May 13 at 0:10

add comment

Here is a solution that I have come up with that solves the shortcomings of instanceof. It can check an object's types from cross-windows and cross-frames and doesn't have problems with primitive types.

f u n c t i o ng e t T y p e ( o ){ r e t u r nO b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( o ) . m a t c h ( / ^ \ [ o b j e c t \ s ( . * ) \ ] $ / ) [ 1 ] ; } f u n c t i o ni s I n s t a n c e ( o b j ,t y p e ){ v a rr e t=f a l s e , i s T y p e A S t r i n g=g e t T y p e ( t y p e )= =" S t r i n g " , f u n c t i o n C o n s t r u c t o r ,i ,l ,t y p e A r r a y ,c o n t e x t ; i f( ! i s T y p e A S t r i n g& &g e t T y p e ( t y p e )! =" F u n c t i o n " ){ t h r o wn e wT y p e E r r o r ( " t y p ea r g u m e n tm u s tb eas t r i n go rf u n c t i o n " ) ; } i f( o b j! = =u n d e f i n e d& &o b j! = =n u l l& &o b j . c o n s t r u c t o r ){ / / g e tt h eF u n c t i o nc o n s t r u c t o r f u n c t i o n C o n s t r u c t o r=o b j . c o n s t r u c t o r ; w h i l e( f u n c t i o n C o n s t r u c t o r! =f u n c t i o n C o n s t r u c t o r . c o n s t r u c t o r ){ f u n c t i o n C o n s t r u c t o r=f u n c t i o n C o n s t r u c t o r . c o n s t r u c t o r ; } / / g e tt h eo b j e c t ' sw i n d o w c o n t e x t=f u n c t i o n C o n s t r u c t o r= =F u n c t i o n?s e l f:f u n c t i o n C o n s t r u c t o r ( " r e t u r nw i n d o w " ) ( ) ; / / g e tt h ec o n s t r u c t o rf o rt h et y p e i f( i s T y p e A S t r i n g ){ / / t y p ei sas t r i n gs ow e ' l lb u i l dt h ec o n t e x t( w i n d o w . A r r a yo rw i n d o w . s o m e . T y p e ) f o r( t y p e A r r a y=t y p e . s p l i t ( " . " ) ,i=0 ,l=t y p e A r r a y . l e n g t h ;i<l& &c o n t e x t ;i + + ){ c o n t e x t=c o n t e x t [ t y p e A r r a y [ i ] ] ; } }e l s e{ / / t y p ei saf u n c t i o ns oe x e c u t et h ef u n c t i o np a s s i n gi nt h eo b j e c t ' sw i n d o w / / t h er e t u r ns h o u l db eac o n s t r u c t o r c o n t e x t=t y p e ( c o n t e x t ) ; } / / c h e c ki ft h eo b j e c ti sa ni n s t a n c eo ft h ec o n s t r u c t o r i f( c o n t e x t ){ r e t=o b ji n s t a n c e o fc o n t e x t ; i f( ! r e t& &( t y p e= =" N u m b e r "| |t y p e= =" S t r i n g "| |t y p e= =" B o o l e a n " ) ){ r e t=o b j . c o n s t r u c t o r= =c o n t e x t } } }

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

6/10

12/12/13
}

} r e t u r nr e t ;

How do I get the name of an object's type in JavaScript? - Stack Overflow

isInstance requires two parameters: an object and a type. The real trick to how it works is that it checks if the object is from the same window and if not gets the object's window. Examples:

i s I n s t a n c e ( [ ] ," A r r a y " ) ;/ / t r u e i s I n s t a n c e ( " s o m es t r i n g " ," S t r i n g " ) ;/ / t r u e i s I n s t a n c e ( n e wO b j e c t ( ) ," O b j e c t " ) ;/ / t r u e f u n c t i o nA n i m a l ( ){ } f u n c t i o nD o g ( ){ } D o g . p r o t o t y p e=n e wA n i m a l ( ) ; i s I n s t a n c e ( n e wD o g ( ) ," D o g " ) ;/ / t r u e i s I n s t a n c e ( n e wD o g ( ) ," A n i m a l " ) ;/ / t r u e i s I n s t a n c e ( n e wD o g ( ) ," O b j e c t " ) ;/ / t r u e i s I n s t a n c e ( n e wA n i m a l ( ) ," D o g " ) ;/ / f a l s e The type argument can also be a callback function which returns a constructor. The callback function will receive one parameter which is the window of the provided object. Examples: / / " A r g u m e n t s "t y p ec h e c k v a ra r g s=( f u n c t i o n ( ){ r e t u r na r g u m e n t s ; } ( ) ) ; i s I n s t a n c e ( a r g s ,f u n c t i o n ( w ){ r e t u r nw . F u n c t i o n ( " r e t u r na r g u m e n t s . c o n s t r u c t o r " ) ( ) ; } ) ;/ / t r u e / / " N o d e L i s t "t y p ec h e c k v a rn l=d o c u m e n t . g e t E l e m e n t s B y T a g N a m e ( " * " ) ; i s I n s t a n c e ( n l ,f u n c t i o n ( w ){ r e t u r nw . d o c u m e n t . g e t E l e m e n t s B y T a g N a m e ( " b s " ) . c o n s t r u c t o r ; } ) ;/ / t r u e One thing to keep in mind is that IE < 9 does not provide the constructor on all objects so the above test for NodeList would return false and also a isInstance(alert, "Function") would return false.
answered Mar 5 '12 at 14:42 Eli 118 1 5 add comment

You can use the i n s t a n c e o f operator to see if an object is an instance of another, but since there are no classes, you can't get a class name.
answered Dec 1 '08 at 22:13 Greg 114k 20 220 261 While it's true that JavaScript doesn't have classes as language construct, the generic convention is still that a type of an object is called a class.. Saul Aug 29 '11 at 6:31 add comment

You can use the "instanceof" operator to determine if an object is an instance of a certain class or not. If you do not know the name of an object's type, you can use its constructor property. The constructor property of objects, is a reference to the function that is used to initialize them. Example: f u n c t i o nC i r c l e( x , y , r a d i u s ){ t h i s . _ x=x ;

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

7/10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


t h i s . _ x=x ; t h i s . _ y=y ; t h i s . _ r a d i u s=r a d u i u s ; } v a rc 1=n e wC i r c l e ( 1 0 , 2 0 , 5 ) ; Now c1.constructor is a reference to the C i r c l e ( ) function. You can alsow use the t y p e o f operator, but the t y p e o f operator shows limited information. One solution is to use the t o S t r i n g ( ) method of the Object global object. For example if you have an object, say myObject, you can use the t o S t r i n g ( ) method of the global Object to determine the type of the class of myObject. Use this: O b j e c t . p r o t o t y p e . t o S t r i n g . a p p l y ( m y O b j e c t ) ;
edited Oct 4 '10 at 10:58 Peter Mortensen 6,772 8 45 75 add comment answered Dec 2 '08 at 18:52 farzad 3,628 2 16 28

Using Object.prototype.toString It turns out, as this post details, you can use Object.prototype.toString - the low level and generic implementation of toString - to get the type for all built-in types O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( ' a b c ' )/ /[ o b j e c tS t r i n g ] O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( / a b c / )/ /[ o b j e c tR e g E x p ] O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( [ 1 , 2 , 3 ] )/ /[ o b j e c tA r r a y ] One could write a short helper function such as f u n c t i o nt y p e ( o b j ) { r e t u r nO b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( o b j ] ) . m a t c h ( / \ s \ w + / ) [ 0 ] . t r i m ( ) } r e t u r n[ o b j e c tS t r i n g ]a sS t r i n g r e t u r n[ o b j e c tN u m b e r ]a sN u m b e r r e t u r n[ o b j e c tO b j e c t ]a sO b j e c t r e t u r n[ o b j e c tU n d e f i n e d ]a sU n d e f i n e d r e t u r n[ o b j e c tF u n c t i o n ]a sF u n c t i o n
answered Mar 24 '12 at 12:26 Dream Factory 496 3 10 add comment

The closest you can get is t y p e o f, but it only returns "object" for any sort of custom type. For those, see Jason Bunting. Edit, Jason's deleted his post for some reason, so just use Object's c o n s t r u c t o r property.
answered Dec 1 '08 at 22:22 sblundy 23.8k 14 67 97 Yeah, sorry - I deleted it because I figured instanceof() was a better way to do things, but I just undeleted it so that it can serve as a reference. Jason Bunting Dec 1 '08 at 22:30 Less than perfect answers are still useful, if only to others to come to the question later because they have a similar problem. So you really shouldn't delete them. Save deletes for wrong answers. sblundy Dec 1 '08 at 22:44 Yeah, I know - you are preaching to the choir, I have said the exact same thing to others. Living those things we know to be true is often harder than it looks. :) Jason Bunting Dec 1 '08 at 22:54 add comment

Use c o n s t r u c t o r . n a m e when you can, and regex function when I can't. F u n c t i o n . p r o t o t y p e . g e t N a m e=f u n c t i o n ( ) { i f( t y p e o ft h i s . n a m e! =' u n d e f i n e d ' ) r e t u r nt h i s . n a m e ; e l s e r e t u r n/ f u n c t i o n( . + ) \ ( / . e x e c ( t h i s . t o S t r i n g ( ) ) [ 1 ] ; } ;
answered Aug 12 '11 at 19:38 defrex 2,243 3 18 31 add comment

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

8/10

12/12/13

How do I get the name of an object's type in JavaScript? - Stack Overflow


The kind() function from Agave.JS will return: the closest prototype in the inheritance tree for always-primitive types like 'null' and 'undefined', the primitive name. It works on all JS objects and primitives, regardless of how they were created , and doesn't have any surprises. Examples:

Numbers

k i n d ( 3 7 )= = =' N u m b e r ' k i n d ( 3 . 1 4 )= = =' N u m b e r ' k i n d ( M a t h . L N 2 )= = =' N u m b e r ' k i n d ( I n f i n i t y )= = =' N u m b e r ' k i n d ( N u m b e r ( 1 ) )= = =' N u m b e r ' k i n d ( n e wN u m b e r ( 1 ) )= = =' N u m b e r '

NaN
k i n d ( N a N )= = =' N a N '

Strings
k i n d ( ' ' )= = =' S t r i n g ' k i n d ( ' b l a ' )= = =' S t r i n g ' k i n d ( S t r i n g ( " a b c " ) )= = =' S t r i n g ' k i n d ( n e wS t r i n g ( " a b c " ) )= = =' S t r i n g '

Booleans
k i n d ( t r u e )= = =' B o o l e a n ' k i n d ( f a l s e )= = =' B o o l e a n ' k i n d ( n e wB o o l e a n ( t r u e ) )= = =' B o o l e a n '

Arrays
k i n d ( [ 1 ,2 ,4 ] )= = =' A r r a y ' k i n d ( n e wA r r a y ( 1 ,2 ,3 ) )= = =' A r r a y '

Objects
k i n d ( { a : 1 } )= = =' O b j e c t ' k i n d ( n e wO b j e c t ( ) )= = =' O b j e c t '

49.7k followers

Dates
k i n d ( n e wD a t e ( ) )= = =' D a t e '

JavaScript is a dynamicallycommonly used for client-sid tag for questions regarding dialects/implementations (ex Unless a tag for a framew or included, a pure JavaScript frequent info

Functions
k i n d ( f u n c t i o n ( ) { } )= = =' F u n c t i o n ' k i n d ( n e wF u n c t i o n ( " c o n s o l e . l o g ( a r g u m e n t s ) " ) )= = =' F u n c t i o n ' k i n d ( M a t h . s i n )= = =' F u n c t i o n '

undefined
k i n d ( u n d e f i n e d )= = =' u n d e f i n e d '

null
k i n d ( n u l l )= = =' n u l l '
answered Oct 23 at 15:04 mikemaccana 5,046 4 32 65 add comment

I was actually looking for a similar thing and came across this question. Here is how I get types: jsfiddle v a rT y p e O f=f u n c t i o n(t h i n g){ v a rt y p e O f T h i n g=t y p e o ft h i n g ; i f(t y p e O f T h i n g= = =' o b j e c t '){ t y p e O f T h i n g=O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( t h i n g ) ; i f(t y p e O f T h i n g= = =' [ o b j e c tO b j e c t ] ' ){ i f(t h i n g . c o n s t r u c t o r . n a m e){ r e t u r nt h i n g . c o n s t r u c t o r . n a m e ; }e l s ei f(t h i n g . c o n s t r u c t o r . t o S t r i n g ( ) . c h a r A t ( 0 )= = =' [ '){ t y p e O f T h i n g=t y p e O f T h i n g . s u b s t r i n g ( 8 , t y p e O f T h i n g . l e n g t h-1 ) ; }e l s e{

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

9/10

12/12/13

}e l s e{ t y p e O f T h i n g=t h i n g . c o n s t r u c t o r . t o S t r i n g ( ) . m a t c h ( / f u n c t i o n \ s * ( \ w + ) / ) ; i f(t y p e O f T h i n g){ r e t u r nt y p e O f T h i n g [ 1 ] ; }e l s e{ r e t u r n' F u n c t i o n ' ; } } }e l s e{ t y p e O f T h i n g=t y p e O f T h i n g . s u b s t r i n g ( 8 , t y p e O f T h i n g . l e n g t h-1 ) ; } } r e t u r nt y p e O f T h i n g . c h a r A t ( 0 ) . t o U p p e r C a s e ( )+t y p e O f T h i n g . s l i c e ( 1 ) ; }

How do I get the name of an object's type in JavaScript? - Stack Overflow

answered Dec 7 at 13:00 Mahdi 2,379 1 9 28 add comment

protected by Engineer Nov 12 at 13:42


This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.

stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript

10/10

You might also like