Package | Top Level |
Class | public class Object |
Subclasses | Accessibility, arguments, Array, AsBroadcaster, BitmapData, BitmapFilter, Boolean, Button, Camera, capabilities, Color, ColorTransform, ContextMenu, ContextMenuItem, CustomActions, Date, Error, ExternalInterface, FileReference, FileReferenceList, Function, IME, Key, LoadVars, LocalConnection, Locale, Math, Matrix, Microphone, Mouse, MovieClip, MovieClipLoader, NetConnection, NetStream, Number, Object, Point, PrintJob, Rectangle, security, Selection, SharedObject, Sound, Stage, String, StyleSheet, System, TextField, TextFormat, TextRenderer, TextSnapshot, Transform, Video, XMLNode, XMLSocket, XMLUI |
Player version: | Flash Player 5 — (became a native object in Flash Player 6, which improved performance significantly). |
Property | ||
---|---|---|
constructor : Object
Reference to the constructor function for a given object instance.
|
||
__proto__ : Object
Refers to the
prototype property of the class (ActionScript 2.0) or constructor function (ActionScript 1.0) used to create the object. |
||
prototype : Object
[static]A reference to the superclass of a class or function object.
|
||
__resolve : Object
A reference to a user-defined function that is invoked if ActionScript code refers to an undefined property or method.
|
Method | ||
---|---|---|
Object()
Creates an Object object and stores a reference to the object's constructor method in the object's
constructor property. |
||
Creates a getter/setter property.
|
||
Indicates whether an object has a specified property defined.
|
||
Indicates whether the specified property exists and is enumerable.
|
||
Indicates whether an instance of the Object class is in the prototype chain of the object specified as an argument.
|
||
[static]Associates a movie clip symbol with an ActionScript object class.
|
||
Converts the specified object to a string and returns it.
|
||
Removes a watchpoint that
Object.watch() created. |
||
Returns the primitive value of the specified object.
|
||
Registers an event handler to be invoked when a specified property of an ActionScript object changes.
|
constructor | property |
public var constructor:Object
Player version: | Flash Player 5 |
Reference to the constructor function for a given object instance. The constructor
property is automatically assigned to all objects when they are created using the constructor for the Object class.
See also
myObject
object. var my_str:String = new String("sven"); trace(my_str.constructor == String); //output: true
If you use the instanceof
operator, you can also determine if an object belongs to a specified class:
var my_str:String = new String("sven"); trace(my_str instanceof String); //output: true
However, in the following example the Object.constructor
property converts primitive data types (such as the string literal seen here) into wrapper objects. The instanceof
operator does not perform any conversion, as seen in the following example:
var my_str:String = "sven"; trace(my_str.constructor == String); //output: true trace(my_str instanceof String); //output: false
__proto__ | property |
public var __proto__:Object
Player version: | Flash Player 5 |
Refers to the prototype
property of the class (ActionScript 2.0) or constructor function (ActionScript 1.0) used to create the object. The __proto__
property is automatically assigned to all objects when they are created. The ActionScript interpreter uses the __proto__
property to access the prototype
property of the object's class or constructor function to find out what properties and methods the object inherits from its superclass. Flex developers should not have to use this property.
See also
// Shape class defined in external file named Shape.as class Shape { function Shape() {} } // Circle class defined in external file named Circle.as class Circle extends Shape{ function Circle() {} }
var oneCircle:Circle = new Circle(); var twoCircle:Circle = new Circle();
__proto_
property of both instances refers to the prototype
property of the Circle class. trace(Circle.prototype == oneCircle.__proto__); // Output: true trace(Circle.prototype == twoCircle.__proto__); // Output: true
prototype | property |
public static var prototype:Object
Player version: | Flash Player 6 |
A reference to the superclass of a class or function object. The prototype
property is automatically created and attached to any class or function object you create. This property is static in that it is specific to the class or function you create. For example, if you create a custom class, the value of the prototype
property is shared by all instances of the class, and is accessible only as a class property. Instances of your custom class cannot directly access the prototype
property, but can access it through the __proto__
property.
See also
// Shape class defined in external file named Shape.as class Shape { function Shape() {} } // Circle class defined in external file named Circle.as class Circle extends Shape{ function Circle() {} }
var oneCircle:Circle = new Circle(); var twoCircle:Circle = new Circle();
prototype
property of the Circle class points to its superclass Shape. The identifier Shape
refers to the constructor function of the Shape class. trace(Circle.prototype.constructor == Shape); // Output: true
prototype
property and the __proto__
property together to move two levels up the inheritance hierarchy (or prototype chain). The Circle.prototype.__proto__
property contains a reference to the superclass of the Shape class. trace(Circle.prototype.__proto__ == Shape.prototype); // Output: true
__resolve | property |
public var __resolve:Object
Player version: | Flash Player 6 |
A reference to a user-defined function that is invoked if ActionScript code refers to an undefined property or method. If ActionScript code refers to an undefined property or method of an object, Flash Player determines whether the object's __resolve
property is defined. If __resolve
is defined, the function to which it refers is executed and passed the name of the undefined property or method. This lets you programmatically supply values for undefined properties and statements for undefined methods and make it seem as if the properties or methods are actually defined. This property is useful for enabling highly transparent client/server communication, and is the recommended way of invoking server-side methods.
See also
__resolve
property. To aid understanding, key statements that differ from the previous usage are in bold typeface. Usage 1: the following example uses __resolve
to build an object where every undefined property returns the value "Hello, world!"
.
// instantiate a new object var myObject:Object = new Object(); // define the __resolve function myObject.__resolve = function (name) { return "Hello, world!"; }; trace (myObject.property1); // output: Hello, world! trace (myObject.property2); // output: Hello, world!
Usage 2: the following example uses __resolve
as a functor, which is a function that generates functions. Using __resolve
redirects undefined method calls to a generic function named myFunction
.
// instantiate a new object var myObject:Object = new Object(); // define a function for __resolve to call myObject.myFunction = function (name) { trace("Method " + name + " was called"); }; // define the __resolve function myObject.__resolve = function (name) { return function () { this.myFunction(name); }; }; // test __resolve using undefined method names myObject.someMethod(); // output: Method someMethod was called myObject.someOtherMethod(); //output: Method someOtherMethod was called
Usage 3: The following example builds on the previous example by adding the ability to cache resolved methods. By caching methods, __resolve
is called only once for each method of interest. This allows lazy construction of object methods. Lazy construction is an optimization technique that defers the creation, or construction, of methods until the time at which a method is first used.
// instantiate a new object var myObject:Object = new Object(); // define a function for __resolve to call myObject.myFunction = function(name) { trace("Method "+name+" was called"); }; // define the __resolve function myObject.__resolve = function(name) { trace("Resolve called for "+name); // to check when __resolve is called // Not only call the function, but also save a reference to it var f:Function = function () { this.myFunction(name); }; // create a new object method and assign it the reference this[name] = f; // return the reference return f; }; // test __resolve using undefined method names // __resolve will only be called once for each method name myObject.someMethod(); // calls __resolve myObject.someMethod(); // does not call __resolve because it is now defined myObject.someOtherMethod(); // calls __resolve myObject.someOtherMethod(); // does not call __resolve, no longer undefined
Usage 4: The following example builds on the previous example by reserving a method name, onStatus()
, for local use so that it is not resolved in the same way as other undefined properties. Added code is in bold typeface.
// instantiate a new object var myObject:Object = new Object(); // define a function for __resolve to call myObject.myFunction = function(name) { trace("Method "+name+" was called"); }; // define the __resolve function myObject.__resolve = function(name) { // reserve the name "onStatus" for local use if (name == "onStatus") { return undefined; } trace("Resolve called for "+name); // to check when __resolve is called // Not only call the function, but also save a reference to it var f:Function = function () { this.myFunction(name); }; // create a new object method and assign it the reference this[name] = f; // return the reference return f; }; // test __resolve using the method name "onStatus" trace(myObject.onStatus("hello")); // output: undefined
Usage 5: The following example builds on the previous example by creating a functor that accepts parameters. This example makes extensive use of the arguments object, and uses several methods of the Array class.
// instantiate a new object var myObject:Object = new Object(); // define a generic function for __resolve to call myObject.myFunction = function (name) { arguments.shift(); trace("Method " + name + " was called with arguments: " + arguments.join(',')); }; // define the __resolve function myObject.__resolve = function (name) { // reserve the name "onStatus" for local use if (name == "onStatus") { return undefined; } var f:Function = function () { arguments.unshift(name); this.myFunction.apply(this, arguments); }; // create a new object method and assign it the reference this[name] = f; // return the reference to the function return f; }; // test __resolve using undefined method names with parameters myObject.someMethod("hello"); // output: Method someMethod was called with arguments: hello myObject.someOtherMethod("hello","world"); // output: Method someOtherMethod was called with arguments: hello,world
Object | () | constructor |
public function Object()
Player version: | Flash Player 5 |
Creates an Object object and stores a reference to the object's constructor method in the object's constructor
property.
var myObject:Object = new Object();
addProperty | () | method |
public function addProperty(name:String, getter:Function, setter:Function):Boolean
Player version: | Flash Player 6 — In ActionScript 2.0 classes, you can use get or set instead of this method. |
Creates a getter/setter property. When Flash reads a getter/setter property, it invokes the get
function, and the function's return value becomes the value of name
. When Flash writes a getter/setter property, it invokes the set
function and passes it the new value as a parameter. If a property with the given name already exists, the new property overwrites it.
A "get" function is a function with no parameters. Its return value can be of any type. Its type can change between invocations. The return value is treated as the current value of the property.
A "set" function is a function that takes one parameter, which is the new value of the property. For example, if property x
is assigned by the statement x = 1
, the set function is passed the parameter 1
of type number. The return value of the set function is ignored.
You can add getter/setter properties to prototype objects. If you add a getter/setter property to a prototype object, all object instances that inherit the prototype object inherit the getter/setter property. This makes it possible to add a getter/setter property in one location, the prototype object, and have it propagate to all instances of a class (similar to adding methods to prototype objects). If a get/set function is invoked for a getter/setter property in an inherited prototype object, the reference passed to the get/set function is the originally referenced object--not the prototype object.
If invoked incorrectly, Object.addProperty()
can fail with an error. The following table describes errors that can occur:
Error condition | What happens |
---|---|
name is not a valid property name; for example, an empty string. | Returns false and the property is not added. |
getter is not a valid function object. | Returns false and the property is not added. |
setter is not a valid function object. | Returns false and the property is not added. |
name:String — A string; the name of the object property to create. |
|
getter:Function — The function that is invoked to retrieve the value of the property; this parameter is a Function object. |
|
setter:Function — The function that is invoked to set the value of the property; this parameter is a Function object. If you pass the value null for this parameter, the property is read-only. |
Boolean —
A Boolean value: true if the property is successfully created; false otherwise.
|
See also
setQuantity()
and getQuantity()
. A property, bookcount
, can be used to invoke these methods when it is either set or retrieved. A third internal method, getTitle()
, returns a read-only value that is associated with the property bookname
. When a script retrieves the value of myBook.bookcount
, the ActionScript interpreter automatically invokes myBook.getQuantity()
. When a script modifies the value of myBook.bookcount
, the interpreter invokes myObject.setQuantity()
. The bookname
property does not specify a set
function, so attempts to modify bookname
are ignored. function Book() { this.setQuantity = function(numBooks:Number):Void { this.books = numBooks; }; this.getQuantity = function():Number { return this.books; }; this.getTitle = function():String { return "Catcher in the Rye"; }; this.addProperty("bookcount", this.getQuantity, this.setQuantity); this.addProperty("bookname", this.getTitle, null); } var myBook = new Book(); myBook.bookcount = 5; trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname); // output: You ordered 5 copies of Catcher in the Rye
The previous example works, but the properties bookcount
and bookname
are added to every instance of the Book
object, which requires having two properties for every instance of the object. If there are many properties, such as bookcount
and bookname,
in a class, they could consume a great deal of memory. Instead, you can add the properties to Book.prototype
so that the bookcount
and bookname
properties exist only in one place. The effect, however, is the same as that of the code in the example that added bookcount
and bookname
directly to every instance. If an attempt is made to access either property in a Book instance, the property's absence will cause the prototype chain to be ascended until the versions defined in Book.prototype
are encountered. The following example shows how to add the properties to Book.prototype
:
function Book() {} Book.prototype.setQuantity = function(numBooks:Number):Void { this.books = numBooks; }; Book.prototype.getQuantity = function():Number { return this.books; }; Book.prototype.getTitle = function():String { return "Catcher in the Rye"; }; Book.prototype.addProperty("bookcount", Book.prototype.getQuantity, Book.prototype.setQuantity); Book.prototype.addProperty("bookname", Book.prototype.getTitle, null); var myBook = new Book(); myBook.bookcount = 5; trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname);
The following example shows how to use the implicit getter and setter functions available in ActionScript 2.0. Rather than defining the Book
function and editing Book.prototype
, you define the Book
class in an external file named Book.as. The following code must be in a separate external file named Book.as that contains only this class definition and resides within the Flash application's classpath:
class Book { var books:Number; function set bookcount(numBooks:Number):Void { this.books = numBooks; } function get bookcount():Number { return this.books; } function get bookname():String { return "Catcher in the Rye"; } }
The following code can then be placed in a FLA file and will function the same way as it does in the previous examples:
var myBook:Book = new Book(); myBook.bookcount = 5; trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname);
hasOwnProperty | () | method |
public function hasOwnProperty(name:String):Boolean
Player version: | Flash Player 6 |
Indicates whether an object has a specified property defined. This method returns true
if the target object has a property that matches the string specified by the name
parameter, and false
otherwise. This method does not check the object's prototype chain and returns true
only if the property exists on the object itself.
name:String |
Boolean —
A Boolean value: true if the target object has the property specified by the name parameter, false otherwise.
|
isPropertyEnumerable | () | method |
public function isPropertyEnumerable(name:String):Boolean
Player version: | Flash Player 6 |
Indicates whether the specified property exists and is enumerable. If true
, then the property exists and can be enumerated in a for..in loop. The property must exist on the target object because this method does not check the target object's prototype chain.
Properties that you create are enumerable, but built-in properties are generally not enumerable.
Parametersname:String |
Boolean —
A Boolean value: true if the property specified by the name parameter is enumerable.
|
See also
Array.length
property, is not enumerable. var myObj:Object = new Object(); myObj.prop1 = "hello"; trace(myObj.isPropertyEnumerable("prop1")); // Output: true var myArray = new Array(); trace(myArray.isPropertyEnumerable("length")); // Output: false
isPrototypeOf | () | method |
public function isPrototypeOf(theClass:Object):Boolean
Player version: | Flash Player 6 |
Indicates whether an instance of the Object class is in the prototype chain of the object specified as an argument. This method returns true
if the object is in the prototype chain of the object specified by the theClass
parameter. The method returns false
not only if the target object is absent from the prototype chain of the theClass
object, but also if the theClass
argument is not an object.
theClass:Object |
Boolean —
A Boolean value: true if the object is in the prototype chain of the object specified by the theClass parameter; false otherwise.
|
registerClass | () | method |
public static function registerClass(name:String, theClass:Function):Boolean
Player version: | Flash Player 6 — If you are using ActionScript 2.0 classes, you can use the ActionScript 2.0 Class field in the Linkage Properties or Symbol Properties dialog box to associate an object with a class instead of using this method. |
Associates a movie clip symbol with an ActionScript object class. If a symbol doesn't exist, Flash creates an association between a string identifier and an object class.
When an instance of the specified movie clip symbol is placed on the Timeline, it is registered to the class specified by the theClass
parameter rather than to the class MovieClip.
When an instance of the specified movie clip symbol is created by using MovieClip.attachMovie()
or MovieClip.duplicateMovieClip()
, it is registered to the class specified by theClass
rather than to the MovieClip class. If theClass
is null
, this method removes any ActionScript class definition associated with the specified movie clip symbol or class identifier. For movie clip symbols, any existing instances of the movie clip remain unchanged, but new instances of the symbol are associated with the default class MovieClip.
If a symbol is already registered to a class, this method replaces it with the new registration.
When a movie clip instance is placed by the Timeline or created using attachMovie()
or duplicateMovieClip()
, ActionScript invokes the constructor for the appropriate class with the keyword this
pointing to the object. The constructor function is invoked with no parameters.
If you use this method to register a movie clip with an ActionScript class other than MovieClip, the movie clip symbol doesn't inherit the methods, properties, and events of the built-in MovieClip class unless you include the MovieClip class in the prototype chain of the new class. The following code creates a new ActionScript class called theClass
that inherits the properties of the MovieClip class:
theClass.prototype = new MovieClip();
name:String — String; the linkage identifier of the movie clip symbol or the string identifier for the ActionScript class. |
|
theClass:Function — A reference to the constructor function of the ActionScript class or null to unregister the symbol. |
Boolean —
A Boolean value: if the class registration succeeds, a value of true is returned; false otherwise.
|
See also
toString | () | method |
public function toString():String
Player version: | Flash Player 5 |
Converts the specified object to a string and returns it.
ReturnsString —
A string.
|
var myObject:Object = new Object(); trace(myObject.toString()); // output: [object Object]
This method can be overridden to return a more meaningful value. The following examples show that this method has been overridden for the built-in classes Date, Array, and Number:
// Date.toString() returns the current date and time var myDate:Date = new Date(); trace(myDate.toString()); // output: [current date and time] // Array.toString() returns the array contents as a comma-delimited string var myArray:Array = new Array("one", "two"); trace(myArray.toString()); // output: one,two // Number.toString() returns the number value as a string // Because trace() won't tell us whether the value is a string or number // we will also use typeof() to test whether toString() works. var myNumber:Number = 5; trace(typeof (myNumber)); // output: number trace(myNumber.toString()); // output: 5 trace(typeof (myNumber.toString())); // output: string
The following example shows how to override toString()
in a custom class. First create a text file named Vehicle.as that contains only the Vehicle class definition and place it into your Classes folder inside your Configuration folder.
// contents of Vehicle.as class Vehicle { var numDoors:Number; var color:String; function Vehicle(param_numDoors:Number, param_color:String) { this.numDoors = param_numDoors; this.color = param_color; } function toString():String { var doors:String = "door"; if (this.numDoors > 1) { doors += "s"; } return ("A vehicle that is " + this.color + " and has " + this.numDoors + " " + doors); } } // code to place into a FLA file var myVehicle:Vehicle = new Vehicle(2, "red"); trace(myVehicle.toString()); // output: A vehicle that is red and has 2 doors // for comparison purposes, this is a call to valueOf() // there is no primitive value of myVehicle, so the object is returned // giving the same output as toString(). trace(myVehicle.valueOf()); // output: A vehicle that is red and has 2 doors
unwatch | () | method |
public function unwatch(name:String):Boolean
Player version: | Flash Player 6 |
Removes a watchpoint that Object.watch()
created. This method returns a value of true
if the watchpoint is successfully removed, false
otherwise.
name:String — A string; the name of the object property that should no longer be watched. |
Boolean —
A Boolean value: true if the watchpoint is successfully removed, false otherwise.
|
See also
Object.watch()
.
valueOf | () | method |
public function valueOf():Object
Player version: | Flash Player 5 |
Returns the primitive value of the specified object. If the object does not have a primitive value, the object is returned.
ReturnsObject —
The primitive value of the specified object or the object itself.
|
See also
// Create a generic object var myObject:Object = new Object(); trace(myObject.valueOf()); // output: [object Object] trace(myObject.toString()); // output: [object Object]
The following examples show the return values for the built-in classes Date and Array, and compares them to the return values of Object.toString()
:
// Create a new Date object set to February 1, 2004, 8:15 AM // The toString() method returns the current time in human-readable form // The valueOf() method returns the primitive value in milliseconds var myDate:Date = new Date(2004,01,01,8,15); trace(myDate.toString()); // output: Sun Feb 1 08:15:00 GMT-0800 2004 trace(myDate.valueOf()); // output: 1075652100000 // Create a new Array object containing two simple elements // In this case both toString() and valueOf() return the same value: one,two var myArray:Array = new Array("one", "two"); trace(myArray.toString()); // output: one,two trace(myArray.valueOf()); // output: one,two
See the example for Object.toString()
for an example of the return value of Object.valueOf()
for a custom class that overrides toString()
.
watch | () | method |
public function watch(name:String, callback:Function, [userData:Object]):Boolean
Player version: | Flash Player 6 |
Registers an event handler to be invoked when a specified property of an ActionScript object changes. When the property changes, the event handler is invoked with myObject
as the containing object.
You can use the return
statement in your callback
method definition to affect the value of the property you are watching. The value returned by your callback
method is assigned to the watched object property. The value you choose to return depends on whether you wish to monitor, modify or prevent changes to the property:
newVal
parameter.oldVal
parameter.If the callback
method you define does not have a return
statement, then the watched object property is assigned a value of undefined.
A watchpoint can filter (or nullify) the value assignment, by returning a modified newval
(or oldval
). If you delete a property for which a watchpoint has been set, that watchpoint does not disappear. If you later recreate the property, the watchpoint is still in effect. To remove a watchpoint, use the Object.unwatch
method.
Only a single watchpoint can be registered on a property. Subsequent calls to Object.watch()
on the same property replace the original watchpoint.
The Object.watch()
method behaves similarly to the Object.watch()
function in JavaScript 1.2 and later. The primary difference is the userData
parameter, which is a Flash addition to Object.watch()
that Netscape Navigator does not support. You can pass the userData
parameter to the event handler and use it in the event handler.
The Object.watch()
method cannot watch getter/setter properties. Getter/setter properties operate through lazy evaluation-- the value of the property is not determined until the property is actually queried. Lazy evaluation is often efficient because the property is not constantly updated; it is, rather, evaluated when needed. However, Object.watch()
needs to evaluate a property to determine whether to invoke the callback
function. To work with a getter/setter property, Object.watch()
needs to evaluate the property constantly, which is inefficient.
Generally, predefined ActionScript properties, such as _x
, _y
, _width
, and _height
, are getter/setter properties and cannot be watched with Object.watch()
.
name:String — A string; the name of the object property to watch. |
|
callback:Function — The function to invoke when the watched property changes. This parameter is a function object, not a function name as a string. The form of callback is callback(prop, oldVal, newVal, userData) . |
|
userData:Object [optional] — An arbitrary piece of ActionScript data that is passed to the callback method. If the userData parameter is omitted, undefined is passed to the callback method. |
Boolean —
A Boolean value: true if the watchpoint is created successfully, false otherwise.
|
See also
watch()
to check whether the speed
property exceeds the speed limit: // Create a new object var myObject:Object = new Object(); // Add a property that tracks speed myObject.speed = 0; // Write the callback function to be executed if the speed property changes var speedWatcher:Function = function(prop, oldVal, newVal, speedLimit) { // Check whether speed is above the limit if (newVal > speedLimit) { trace ("You are speeding."); } else { trace ("You are not speeding."); } // Return the value of newVal. return newVal; } // Use watch() to register the event handler, passing as parameters: // - the name of the property to watch: "speed" // - a reference to the callback function speedWatcher // - the speedLimit of 55 as the userData parameter myObject.watch("speed", speedWatcher, 55); // set the speed property to 54, then to 57 myObject.speed = 54; // output: You are not speeding myObject.speed = 57; // output: You are speeding // unwatch the object myObject.unwatch("speed"); myObject.speed = 54; // there should be no output