56 | Chapter 3: Working with JavaScript and HTML Within AIR
//call a function on an instance of a class
var file = desktop.resolve("output.txt");
//create a new instance of a class using new
var fileStream = new window.runtime.flash.filesystem.
FileStream( );
//call a function, passing arguments
fileStream.open(file, window.runtime.flash.filesystem.
FileMode.WRITE);
fileStream.writeUTFBytes("Hello World");
fileStream.close( );
Don’t worry too much about what the code is doing in this
example, but rather focus on how the AIR APIs are called
from JavaScript.
This allows you to leverage virtually any AIR or Flash Player
API from within JavaScript.
TIP
Download JavaScript specific language references for AIR
from the Adobe web site at:http://www.adobe.com/go/air.
By remembering how to use the package structure to call
APIs, you can leverage all AIR, Flash Player and ActionScript
APIs even if JavaScript-specific documentation is not
provided.
Working with AIR and Flash Player Events
Many of the AIR and Flash Player APIs make extensive use of
events. Event handling in ActionScript-based APIs is based
on the W3C DOM Level 3 event model. This is similar to the
W3C DOM Level 2 event model available within JavaScript,
but is very different from the callback mechanism often
deployed in JavaScript.
In order to be notified when an event from an AIR or Flash
Player API occurs, you must register to listen for it. The best