Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
typeName() function -- introspecting object types
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Per Cederberg  
View profile  
 More options Jan 10 2011, 5:52 am
From: Per Cederberg <cederb...@gmail.com>
Date: Mon, 10 Jan 2011 11:52:58 +0100
Local: Mon, Jan 10 2011 5:52 am
Subject: typeName() function -- introspecting object types
Being tired of all the idiosyncrasies of the typeof() operator, I
tried to make something better:

   function typeName(value) {
       if (typeof(value) !== "object" && !(value instanceof RegExp)) {
           return typeof(value);
       } else if (value == null) {
           return "null";
       } else {
           var c = value.constructor || {};
           return c.name || c.NAME || "Object";
       }
   }

See here:

https://github.com/cederberg/mochikit/commit/582a51531f68a17aa0ddea41...

I was thinking about including this in MochiKit.Base, possibly
modifying typeMatcher() on the go. That would break backward
compatibility a bit, so the question is if it would be worthwhile?

This is how typeName() works right now:

 undefined ==> "undefined"
 null ==> "null"
 false ==> "boolean"
 new Boolean(true) ==> "Boolean"
 42 ==> "number"
 new Number(42) ==> "Number" [1]
 "test" ==> "string"
 new String("") ==> "String" [1]
 { a: 1 } ==> "Object" [2]
 [1,2,3] ==> "Array"
 new Date() ==> "Date"
 /\d+/ ==> "RegExp"
 new MyClass() ==> "MyClass" [3]

Notes:
[1]: There are two forms of these built-in types, but normally the
"constructor" form is not used much.
[2]: These objects constructor actually point to the Object function,
hence the upper-case initial.
[3]: For this to work, the constructor property must be set and the
constructor function must have either a name or a NAME property.

Cheers,

/Per


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »