How Do I Get The Name of An Object's Type in JavaScript - Stack Overflow
How Do I Get The Name of An Object's Type in JavaScript - Stack Overflow
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Tell me more
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...
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
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
stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript
2/10
12/12/13
/ /t r u e / /t r u e / /t r u 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
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
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
stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript
4/10
12/12/13
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
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:
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 ;
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
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
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
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