Skip to content

Instantly share code, notes, and snippets.

@alecmce
Created December 3, 2012 21:13
Show Gist options
  • Save alecmce/4198110 to your computer and use it in GitHub Desktop.
Save alecmce/4198110 to your computer and use it in GitHub Desktop.
How do you test for membership in AS3?
// option 1
private static const ABC:Object = {"a":null, "b":null, "c":null}
private function isABC(char:String):Boolean
{
return char in ABC;
}
// option 2
private static const ABC:Vector.<String> = new <String>["a","b","c"];
private function isABC(char:String):Boolean
{
return ABC.indexOf(char) != -1;
}
// option 3? - other than using an Array, assuming that the ABC list could grow so we're not going to hard-code values, what other options do we have?
@darscan
Copy link

darscan commented Dec 3, 2012

dictionary[key] !== undefined

@quentint
Copy link

quentint commented Dec 3, 2012

Option 2 for me.
Scholar but readable!

@mnem
Copy link

mnem commented Dec 3, 2012

I'd go for 1 I think, although with the modification of {"a":true, "b":true, "c":true} and just return ABC[char]. Although I suspect that's a pointless implementation detail that misses the point of this question. I just can't help it :)

2 is more type-safe (and possibly more "engineering" AS idiomatic), and so perhaps less liable to have random bugs introduced by people new to the code.

On the other hand, if the main interface is isABC, are the implementation details that important?

@alecmce
Copy link
Author

alecmce commented Dec 3, 2012

Thanks for the feedback! I much prefer Dave's implementation with {"a":true, "b":true, "c":true} to #1. Always fun to ruminate on these details :)

@tdavies
Copy link

tdavies commented Dec 4, 2012

Depends if speed is an issue. Object or dictionary access is going to be faster as its a hash map (thought i'm not sure about using "in"). Pretty sure indexOf just checks everything from the start index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment