Manipulating Data with ActionScript in Flex Applications - Objects
(Page 4 of 5 )
Objects are composites of state and functionality that you can use as elements within ActionScript code. There are potentially an infinite range of object types, including those from the built-in Flash Player types to Flex framework types to custom types.
An object is an instance of a class, which is a blueprint of sorts. Although there are other mechanisms for creating objects, the most common is to use anewstatement with a constructor. The constructor for a class is a special function that shares the same name as the class. For example, the constructor for theArray class is calledArray. Like any other functions a constructor may or may not expect parameters. The only way to know whether a particular constructor expects parameters is to consult the API documentation. However, unlike most functions, a constructor must be used as part of anew statement, and it always creates a new instance of the class. The following example creates a new array using anewstatement:
var books:Array = new Array();
Objects may have properties and methods depending on the type. Properties are essentially variables associated with an object, and methods are essentially functions associated with the object. You can reference properties and methods of an object in ActionScript using dot-syntax. Dot-syntax uses a dot between the name of the object and the property of the method. The following example uses dot-syntax to call thepush() method of the array object (thepush()method appends the value as an array element):
books.push("Programming Flex 2");
The next example uses dot-syntax to reference thelengthproperty of the array object:
trace(books.length);
Next: Inheritance >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|