Chat

We have 5 guests online.

Please login to be able to chat.

Login



Designed by:
Muhammad bin Yusrat ibn Yusrat

How to check data type in AS3 (the IS operator)
(7 votes, average 3.71 out of 5)

 

Many a times I’ve found myself in a situation where I need to check if a variable has a particular data type, and accordingly perform some action. I could not find anything by searching for this for quite some time until one day I stumbled across this beautiful ‘operator’ that does it (in someone else's code). Here is a brief article about checking to see if a variable belongs to a specific data type or class.

Meet, the ‘is’ operator

I have found this operator to be really beautiful, and if you have some other programmer friend sitting with you, you can impress him by using this (if they don’t already know!). Here is a basic example:

var c:Boolean = new Boolean();
if(c is Boolean){
trace("Yes, c is Boolean");
}

Some of you might be wondering why would we need this thing at the first place? Since all the variables we use already have a datatype defined, whats the need of checking its data type? Well, here are a few examples:

stage.addEventListener(MouseEvent.CLICK,MouseClickListener);
function MouseClickListener(e:Event):void{
if(e.target is Sprite){
trace("You clicked on a sprite!");
}
else if(e.target is stage){
trace("You didn't click anything.");
}

Another way to write the above function in a lot fewer lines would be:

function MouseClickListener(e:Event):void{
var Response:String = e.target is Sprite ? "Sprite." : "nothing.";
trace("You clicked" + Response);
}

Some of you might be wondering what did I do with line number 2 here. Its called "ternary" operation. Its basically a fancy (and brief) way of writing: if e.target is Sprite, then return "sprite" else return "nothing". You have probably guessed, this operator can prove to be very useful in some situations. You can also define a variable without a datatype, in those cases this operator almost becomes a necessity. For instance:

var myVar:*;
myvar = "Hello!";
//myvar = 393;
if(myvar is String){
trace("Data type detected: " + (myvar is String ? "String" : "Number"));
}

You can try commenting out the second line and uncommenting the third to see different results.

If you have questions, please feel free to post in the forums and I will see you next time!

 

 

Comments 

 
0 #2 Jason Nash 2011-06-03 14:15
Hi,

I am writing to follow up on my last email regarding our sensible proposal for your site because I did not receive a response.
We have had a server problem lately and I’m afraid I couldn’t read your email.

We just want to find out if your are open in discussing a sponsorship proposal for your page.


Best regards,
Jason Nash
Quote
 
 
0 #1 Jason Nash 2011-05-31 16:39
Hello,

I am Jason and I represent the most trusted low-cost web hosting reviews in the market.
I am really amused with the way you present your ideas/concepts on your site.


Would you be open in discussing a sensible business proposal or sponsorships?


Thanks,

Jason Nash
Quote
 

Add comment


Security code
Refresh