x360c
says
How could I go about telling if a variable is an array or a string?
3867 posts
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
flashjunkie
says
var variablename:String = “blah”;
trace(typeof(variablename));
x360c
says
Yeah, but that returns as an object when it’s an array. Null is also classified as an object so it’s conflicting. Any other methods?
x360c
says
Any solutions?
djankey
says
var myString:String="djankey";
var myArray:Array=["djankey", "as-flash.com"];
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;
}
trace(getTypeOf(myString));
trace(getTypeOf(myArray));senocular: getTypeOf() – typeof alternative, recognizes more object types
JoeMC
says
if (myVariable is Array){ trace(“true”); }
x360c
says
Go Joe! Thanks man.
