As we’ve seen, MXML is great for laying out structure, and ActionScript is suited for defining method logic and interaction. So what is the difference between the two? In many ways they’re the same thing.
MXML and ActionScript complement each other. You’ll find it’s easier to build an application’s structure using MXML than using just ActionScript. MXML unfolds faster, it’s easier to read and write, and you get results from fewer keystrokes. But MXML doesn’t replace ActionScript. Scripting is quite necessary for serious Flex applications. In fact, if anything it’s MXML that’s unnecessary, because if you wanted, you could build a full application using just ActionScript.
When you compile an application, all the MXML you created is initially interpreted into ActionScript, and then it’s processed into a binary SWF. You can think of it like this: ActionScript is the language of Flash Player, and everything in Flex is eventually distilled into ActionScript.
MXML components in a Flex application can be reduced to equivalent ActionScript code. For example, the following code creates a
Button in MXML:
But you can also add a
Button using ActionScript:
Knowing this, you can use ActionScript to create components dynamically, without having to rely on MXML.