Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

getTypeOf.as

Name: getTypeOf() - typeof alternative, recognizes more object types
Author: senocular: www.senocular.com
Date: 1899-12-31T00:32:20.500
Documentation:
Object GETTYPEOF: extends the typeof allowing for a better
recoginition of different kinds of objects.
 
Arguments:
- obj: passed if used as a standalone function; the object to return the type of
 
Returns:
- returns the type of object specified.  If no type is discovered, null is returned

Note:
- Since this is an Object prototype, it can be used anywhere and as it is set up can have two
forms of usage: object.getTypeOf() and getTypeOf(object). Both are used in the example above.
- The array of objects listed in this prototype uses capitolization, be aware of that if 
comparing object types.
- (MX) also be aware of instanceof for similar functionality.  This will work with Flash 5.
 
Hints:
- You can use this on your on custom objects as well as long as you put your object constructor
in the array within this prototype.  Ex:
 
monkey = function(){
	this.fun = "much";
}
bubbles = new Monkey();
trace( getTypeOf(bubbles) ); // traces null
 
put monkey in the t array...
 
...
var t = ["monkey", "Array", ...]
...
 
then test
 
trace( getTypeOf(bubbles) ); // traces monkey

Example:
objs = [	new Array(),
	new Boolean(),
	new Number(),
	new Object(),
	new String(),
	[],
	false,
	1,
	{},
	"",
	new Color(),
	new Date(),
	new XML()];
 
for(i=0; i

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// constructor compare
Object.prototype.getTypeOf = function(obj){
	if (arguments.length) var my = obj;
	else var my = this;
	var t = ["Array","Boolean","Button","Color","Date","Function","LoadVars","MovieClip","Number","Object","Sound","String","TextField","TextFormat","XML","XMLSocket"];
	var i, l = t.length;
	for(i=0; i<l; i++) if (my.constructor == new [t[i]]().constructor) return t[i];
	return null;
}
// or
// __proto__ compare
Object.prototype.getTypeOf = function(obj){
	if (arguments.length) var my = obj;
	else var my = this;
	var t = [Array,Boolean,Button,Color,Date,Function,LoadVars,MovieClip,Number,Object,Sound,String,TextField,TextFormat,XML,XMLSocket];
	var i, l = t.length;
	for(i=0; i<l; i++) if (my.__proto__ == t[i].prototype) return t[i];
	return null;
}