How do you get an instance of the actionscript class Class
from an instance of that class?
In Python, this would be x.__class__
; in Java, x.getClass();
.
I'm aware that certain terrible hacks exist to do this, but I'm looking for a built-in language facility, or at least a library routine built on something reliable.
Any reason you couldn't do this?
I don't know a way to avoid round-tripping through a String, but it should work well enough.
You can get it through the 'constructor' property of the base Object class. i.e.:
The accepted (and currently most popular answer) has some flaws. The answer serves for this specific use case, but the comments have expanded the answer to a seeming general solution.
But it is not a type-safe solution in certain cases, and it doesn't address all possible objects. The idea that XML is not supported has been addressed well enough here and elsewhere, but the type-safe idea has not.
The assumption made is that it is an class object created by the programmer. Here are some tests that I set up (this is in strict mode, but a local test). Note the
int
test results:With
TestClass
defined as:Results:
Beyond any current testing, there is fairly good reason to use
getDefinitionByName
over theconstructor
methods. ThegetDefinitionByName
method allows :It may be slower now, but in the future there may be improvements from Adobe's side that would address the speed issue.
(For example, it used to be that
uint
was far slower in for-loops thanint
, so a lot of conversion code was set up to use the fasterint
. It was a fairly simple issue to solve, so Adobe fixed it, and now there is a lot of code that jumps through some unnecessary hoops to achieve an outdated goal.)Since the
getDefinitionByName
method works correctly in all cases, code should be developed using that method, and then work to get Adobe to fix the speed issue.