Runtime version: | AIR 1.0 |
new
operator syntax, and can have properties assigned to them dynamically.
Objects can also be created by
assigning an object literal, as in:
var obj:Object = {a:"foo", b:"bar"}
All classes that don't declare an explicit base class extend the built-in Object class.
All runtime classes in the Adobe Integrated Runtime extend the runtime.Object class.
You can use the Object class to create associative arrays. At its core, an associative array is an instance of the Object class, and each key-value pair is represented by a property and its value. Another reason to declare an associative array using the Object data type is that you can then use an object literal to populate your associative array (but only at the time you declare it). The following example creates an associative array using an object literal, accesses items using both the dot operator and the array access operator, and then adds a new key-value pair by creating a new property:
var myAssocArray:Object = {fname:"John", lname:"Public"}; trace(myAssocArray.fname); // John trace(myAssocArray["lname"]); // Public myAssocArray.initial = "Q"; trace(myAssocArray.initial); // Q
ActionScript 3.0 has two types of inheritance: class inheritance and prototype inheritance:
Both class inheritance and prototype inheritance can exist simultaneously, as shown in the following example:
class A { var x = 1 prototype.px = 2 } dynamic class B extends A { var y = 3 prototype.py = 4 } var b = new B() b.x // 1 via class inheritance b.px // 2 via prototype inheritance from A.prototype b.y // 3 b.py // 4 via prototype inheritance from B.prototype B.prototype.px = 5 b.px // now 5 because B.prototype hides A.prototype b.px = 6 b.px // now 6 because b hides B.prototype
Using functions instead of classes, you can construct custom prototype inheritance trees. With classes, the prototype inheritance tree mirrors the class inheritance tree. However, since the prototype objects are dynamic, you can add and delete prototype-based properties at run time.
See also
Property | Defined by | ||
---|---|---|---|
constructor : Object
A reference to the class object or constructor function for a given object instance.
| Object | ||
prototype : Object
[static]
A reference to the prototype object of a class or function object.
| Object |
Method | Defined by | ||
---|---|---|---|
Object()
Creates an Object object and stores a reference to the object's constructor method in the object's
constructor property. | Object | ||
Indicates whether an object has a specified property defined.
| Object | ||
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter.
| Object | ||
Indicates whether the specified property exists and is enumerable.
| Object | ||
Sets the availability of a dynamic property for loop operations.
| Object | ||
Returns the string representation of this object, formatted according to locale-specific conventions.
| Object | ||
Returns the string representation of the specified object.
| Object | ||
Returns the primitive value of the specified object.
| Object |
constructor | property |
public var constructor:Object
Runtime version: | AIR 1.0 |
A reference to the class object or constructor function for a given object instance.
If an object is an instance of a class, the constructor
property holds a reference to the class object.
If an object is created with a constructor function, the constructor
property holds a reference to the constructor function.
Do not confuse a constructor function with a constructor method of a class.
A constructor function is a Function object used to create objects, and is an
alternative to using the class
keyword for defining classes.
If you use the class
keyword to define a class, the class's prototype object
is assigned a property named constructor
that holds a reference to the class object.
An instance of the class inherits this property from the prototype object. For example,
the following code creates a new class, A
, and a class instance named myA
:
dynamic class A {} trace(A.prototype.constructor); // [class A] trace(A.prototype.constructor == A); // true var myA:A = new A(); trace(myA.constructor == A); // true
Advanced users may choose to use the function
keyword instead of the class
keyword to define a Function object that can be used as a template for creating objects. Such a
function is called a constructor function because you can use it in conjunction with the new
operator to create objects.
If you use the function
keyword to create a constructor function, its prototype object is assigned
a property named constructor
that holds a reference to the constructor function.
If you then use the constructor function to create an object, the object inherits the
constructor
property from the constructor function's prototype object. For example,
the following code creates a new constructor function, f
, and an object named myF
:
function f() {} trace(f.prototype.constructor); // function Function() {} trace(f.prototype.constructor == f); // true var myF = new f(); trace(myF.constructor == f); // true
Note: The constructor
property is writable, which means that user code can change
its value with an assignment statement. Changing the value of the constructor
property is not
recommended, but if you write code that depends on the value of the constructor
property, you should
ensure that the value is not reset. The value can be changed only when the property is accessed through the prototype
object (for example, className.prototype.constructor
).
If you access the constructor
property and compile in strict mode, you will get an error at compile
time because the constructor property depends on the protoype object, which is a runtime entity.
If you use standard mode or if the class description specifies "dynamic", the code runs without generating
an error.
See also
prototype | property |
public static var prototype:Object
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
A reference to the prototype object of a class or function object. The prototype
property
is automatically created and attached to any class or function object that you create. This property is
static in that it is specific to the class or function that you create. For example, if you create a
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 class cannot directly access
the prototype
property.
A class's prototype object is a special instance of that class that provides a mechanism for sharing state across all instances of a class. At run time, when a property is not found on a class instance, the delegate, which is the class prototype object, is checked for that property. If the prototype object does not contain the property, the process continues with the prototype object's delegate checking in consecutively higher levels in the hierarchy until the Flash runtime finds the property.
Note: In ActionScript 3.0, prototype inheritance is not the primary mechanism for inheritance. Class inheritance, which drives the inheritance of fixed properties in class definitions, is the primary inheritance mechanism in ActionScript 3.0.
Object | () | constructor |
public function Object()
Runtime version: | AIR 1.0 |
Creates an Object object and stores a reference to the object's constructor method in the object's constructor
property.
hasOwnProperty | () | method |
AS3 function hasOwnProperty(name:String):Boolean
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
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.
The following types of properties cause this method to return true
for objects that are instances of a class (as opposed to class objects):
dynamic
keyword.The following types of properties cause this method to return false
for objects that are instances of a class:
valueOf()
method because it exists on Object.prototype
, which is part of the prototype chain for the Array class. Although you can use valueOf()
on an instance of Array, the return value of hasOwnProperty("valueOf")
for that instance is false
.ActionScript 3.0 also has class objects, which are direct representations of class definitions.
When called on class objects, the hasOwnProperty()
method returns true
only if a property
is a static property defined on that class object. For example, if you create a subclass of Array named
CustomArray, and define a static property in CustomArray named foo
, a call to
CustomArray.hasOwnProperty("foo")
returns true
.
For the static property DESCENDING
defined in the Array class, however, a call to
CustomArray.hasOwnProperty("DESCENDING")
returns false
.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override
keyword. For example, A subclass of Object implements function hasOwnProperty():Boolean
instead of using an override of the base class.
name:String — The property of the object.
|
Boolean —
If the target object has the property specified by the name
parameter this value is true , otherwise false .
|
isPrototypeOf | () | method |
AS3 function isPrototypeOf(theClass:Object):Boolean
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. This method returns true
if the object is in the prototype chain of the
object specified by the theClass
parameter. The method returns false
if the target object is absent from the prototype chain of the theClass
object,
and also if the theClass
parameter is not an object.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override
keyword. For example, A subclass of Object implements function isPrototypeOf():Boolean
instead of using an override of the base class.
theClass:Object — The class to which the specified object may refer.
|
Boolean —
If the object is in the prototype chain of the object
specified by the theClass parameter this value is true , otherwise false .
|
propertyIsEnumerable | () | method |
AS3 function propertyIsEnumerable(name:String):Boolean
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
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.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override
keyword. For example, A subclass of Object implements function propertyIsEnumerable():Boolean
instead of using an override of the base class.
name:String — The property of the object.
|
Boolean —
If the property specified by the name parameter is enumerable this value is true , otherwise false .
|
setPropertyIsEnumerable | () | method |
public function setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
Sets the availability of a dynamic property for loop operations. The property must exist on the target object because this method does not check the target object's prototype chain.
Parametersname:String — The property of the object.
|
|
isEnum:Boolean (default = true ) — If set to false , the dynamic property does not show up in for..in loops, and the method propertyIsEnumerable() returns false .
|
See also
toLocaleString | () | method |
public function toLocaleString():String
Language version: | ActionScript 3.0 |
Runtime version: | AIR 1.0 |
Returns the string representation of this object, formatted according to locale-specific conventions.
The default implementation of this method does not perform locale-specific formatting and returns the
same string as toString()
. Subclasses should provided their own locale-aware implementation when appropriate.
Note: Methods of the Object class are dynamically created on Object's prototype.
To redefine this method in a subclass of Object, do not use the override
keyword. For example,
a subclass of Object implements function toLocaleString():String
instead of using an override of the base class.
String —
A string representation of this object formatted according to local conventions.
|
See also
toString | () | method |
public function toString():String
Runtime version: | AIR 1.0 |
Returns the string representation of the specified object.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override
keyword. For example, a subclass of Object implements function toString():String
instead of using an override of the base class.
String —
A string representation of the object.
|
valueOf | () | method |
public function valueOf():Object
Runtime version: | AIR 1.0 |
Returns the primitive value of the specified object. If this object does not have a primitive value, the object itself is returned.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override
keyword. For example, A subclass of Object implements function valueOf():Object
instead of using an override of the base class.
Object —
The primitive value of this object or the object itself.
|
See also
ObjectExample
and Circle
to demonstrate the dynamic nature of the Object class, and how value objects can be transformed into
Shape objects and then added to the stage at the specified x/y coordinates.
The example creates the value objects firstInitObj
and secondInitObj
. The custom
class Circle
accepts the value object and loops over it while setting its matching internal
properties to those defined in the value object.
package { import flash.display.Sprite; public class ObjectExample extends Sprite { public function ObjectExample() { var firstInitObj:Object = new Object(); firstInitObj.bgColor = 0xFF0000; firstInitObj.radius = 25; firstInitObj.xCenter = 25; firstInitObj.yCenter = 25; var firstCircle:Circle = new Circle(firstInitObj); addChild(firstCircle); firstCircle.x = 50; firstCircle.y = 50; var secondInitObj:Object = {bgColor:0xCCCCCC, radius:50, xCenter:50, yCenter:50}; var secondCircle:Circle = new Circle(secondInitObj); addChild(secondCircle); secondCircle.x = 100; secondCircle.y = 100; } } } import flash.display.Shape; class Circle extends Shape { public var bgColor:Number = 0xFFFFFF; public var radius:Number = 0; public var xCenter:Number = 0; public var yCenter:Number = 0; public function Circle(initObj:Object) { for(var i:String in initObj) { this[i] = initObj[i]; } draw(); } public function draw():void { graphics.beginFill(bgColor); graphics.drawCircle(xCenter, yCenter, radius); graphics.endFill(); } }