ActionScript for Flash MX: The Definitive Guide, Second Edition

ActionScript for Flash MX: The Definitive Guide, 2nd EditionBy Colin Moock
Chapter 18.  ActionScript Language Reference
Function.prototype Property Flash 5

an object specifying methods and properties for a class read/write

theFunction.prototype

Description

The prototype property is the centerpiece of ActionScript's class and object inheritance system. By default, it is a generic object waiting to be assigned properties and methods that will become available to objects constructed by theFunction. Like any generic object, theFunction.prototype starts off with the built-in properties _ _proto_ _ and constructor. ActionScript automatically ensures that theFunction.prototype.constructor refers to theFunction (in all other situations, an object's constructor property initially refers to its own constructor function, which in the case of a generic object would be Object, not theFunction).

The prototype object can also be replaced by a new object, as is commonly done when specifying a class's superclass.

The Function class itself is also a function, so it too defines a prototype property that can be used to add methods and properties to all functions. For example, the following code adds a getName( ) and setName( ) method to all functions, and overrides the default Function.toString( ) method:

Function.prototype.getName = function () { return this.funcName; }; Function.prototype.setName = function (name) { this.funcName = name; }; Function.prototype.toString = Function.prototype.getName; // Usage: function squareNum (x) { return x * x; } squareNum.setName("squareNum"); trace(squareNum); // Displays: squareNum // (instead of [type Function])

For a full discussion of prototype's role in object-oriented ActionScript programming, see Chapter 12. Within the advanced developer community, the prototype property is the focus of a lively debate on superclass assignment methodology. See Section 12.7.3.

See Also

Object.constructor, Object._ _proto_ _; Chapter 12

    Категории