顶级
public dynamic class Function
继承Function Inheritance Object

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

函数是可在 ActionScript 中调用的基本代码单位。 ActionScript 中用户定义的函数和内置函数都由 Function 对象来表示,该对象是 Function 类的实例。

类的方法与 Function 对象略有不同。 与普通函数对象不同,方法和与其关联的类对象紧密关联。 因此,方法或属性具有在同一类的所有实例中共享的定义。 可以从实例提取方法并将其处理为“绑定”方法(保留与原始实例的链接)。 对于绑定方法,this 关键字指向实现该方法的原始对象。 对于函数,this 在调用函数时指向关联对象。

查看示例

另请参见

方法



公共 属性
 属性定义方
 Inheritedconstructor : Object
对类对象或给定对象实例的构造函数的引用。
Object
 Inheritedprototype : Object
[static] 对类或函数对象的原型对象的引用。
Object
公共 方法
 方法定义方
  
apply(thisObject:Object, argArray:Array = null):void
指定要在 ActionScript 调用的任何函数内使用的 thisObject 的值。
Function
  
call(thisObject:Object, parameter1:String = null):void
调用 Function 对象表示的函数。
Function
 Inherited
指示对象是否已经定义了指定的属性。
Object
 Inherited
指示 Object 类的实例是否在指定为参数的对象的原型链中。
Object
 Inherited
指示指定的属性是否存在、是否可枚举。
Object
 Inherited
设置循环操作动态属性的可用性。
Object
 Inherited
返回指定对象的字符串表示形式。
Object
 Inherited
返回指定对象的原始值。
Object
方法详细信息
apply()方法
AS3 function apply(thisObject:Object, argArray:Array = null):void

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

指定要在 ActionScript 调用的任何函数内使用的 thisObject 的值。 此方法还指定要传递给任何被调用函数的参数。 由于 apply() 是 Function 类的方法,所以它也是 ActionScript 中每个 Function 对象的方法。

Function.call() (它将参数指定为用逗号分隔的列表)不同,该方法将参数指定为一个 Array 对象。 如果在脚本实际执行前,无法知道要传递的参数的数量,那么这种方法通常很有用。

返回被调用函数指定为返回值的值。

参数

thisObject:Object — 要应用该函数的对象。
 
argArray:Array (default = null) — 其元素作为参数传递给函数的数组。

另请参见

call()方法 
AS3 function call(thisObject:Object, parameter1:String = null):void

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

调用 Function 对象表示的函数。 ActionScript 中的每个函数都由一个 Function 对象来表示,所以所有的函数都支持此方法。

几乎在所有的情形下,都可以使用函数调用运算符 (()) 来代替此方法。 函数调用运算符使代码简明易读。 如果需要显式控制函数调用中的 thisObject 参数,则此方法很有用。 通常,如果在函数体内将函数作为对象的方法来调用,则 thisObject 设置为 myObject,如下例所示:

  myObject.myMethod(1, 2, 3);
  

在某些情况下,您可能希望 thisObject 指向其它地方;例如,函数必须作为对象的方法进行调用,但该函数实际上并不作为该对象的方法进行存储。

  myObject.myMethod.call(myOtherObject, 1, 2, 3); 
  

可以为 thisObject 参数传递值 null,以便将函数作为一个常规函数而非对象的方法来调用。 例如,以下函数调用是等效的:

  Math.sin(Math.PI / 4)
  Math.sin.call(null, Math.PI / 4)
  

返回被调用函数指定为返回值的值。

参数

thisObject:Object — 指定函数体内 thisObject 值的对象。
 
parameter1:String (default = null) — 要传递给该函数的参数。 可以指定 0 个或多个参数。

另请参见

示例 如何使用示例

下例使用 FunctionExampleSimpleCollectionEventBroadcasterEventListener 类显示 ActionScript 中函数的各种用法。 这是由以下步骤完成的:
  1. FunctionExample 的构造函数创建一个名为 simpleColl 的局部变量,该变量用一个整数数组填充,其范围从 18
  2. 使用 trace() 打印 simpleColl 对象。
  3. 将 EventListener 对象 listener 添加到 simpleColl 中。
  4. 调用 insert()remove() 函数时,侦听器响应其事件。
  5. 创建第二个 SimpleCollection 对象,名为 greaterThanFourColl
  6. 赋予 greaterThanFourColl 对象 simpleColl.select() 的结果,后者包含参数 4 和一个匿名函数。 SimpleCollection 对象的选择方法是一个内部迭代器,它使用匿名函数参数作为块。
package {
    import flash.display.Sprite;
    
    public class FunctionExample extends Sprite {
        public function FunctionExample() {
            var simpleColl:SimpleCollection;
            simpleColl = new SimpleCollection(0, 1, 2, 3, 4, 5, 6, 7, 8);
            trace(simpleColl);        // 0, 1, 2, 3, 4, 5, 6, 7, 8

            var listener:EventListener = new EventListener();
            simpleColl.addListener(listener);
            simpleColl.insert(9);        // itemInsertedHandler: 9
            simpleColl.remove(8);        // itemRemovedHandler: 8
            trace(simpleColl);        // 0, 1, 2, 3, 4, 5, 6, 7, 9

            var greaterThanFourColl:SimpleCollection;
            greaterThanFourColl = simpleColl.select(4, function(item:int, value:int){ return item > value });
            trace(greaterThanFourColl);    // 5, 6, 7, 9
        }
    }
}
    
import flash.display.Sprite;
    
class EventBroadcaster {
    private var listeners:Array;

    public function EventBroadcaster() {
        listeners = new Array();
    }
        
    public function addListener(obj:Object):void {
        removeListener(obj);
        listeners.push(obj);
    }
        
    public function removeListener(obj:Object):void {
        for(var i:uint = 0; i < listeners.length; i++) {
            if(listeners[i] == obj) {
                listeners.splice(i, 1);
            }
        }
    }
    
    public function broadcastEvent(evnt:String, ...args):void {
        for(var i:uint = 0; i < listeners.length; i++) {
            listeners[i][evnt].apply(listeners[i], args);
        }
    }    
}
    
class SimpleCollection extends EventBroadcaster {
    private var arr:Array;
        public function SimpleCollection(... args) {
        arr = (args.length == 1 && !isNaN(args[0])) ? new Array(args[0]) : args;
    }
        
    public function insert(obj:Object):void {
        remove(obj);
        arr.push(obj);
        broadcastEvent("itemInsertedHandler", obj);
    }
        
    public function remove(obj:Object):void {
        for(var i:uint = 0; i < arr.length; i++) {
            if(arr[i] == obj) {
                var obj:Object = arr.splice(i, 1)[0];
                broadcastEvent("itemRemovedHandler", obj);
            }
        }
    }

    public function select(val:int, fn:Function):SimpleCollection {
        var col:SimpleCollection = new SimpleCollection();
        for(var i:uint = 0; i < arr.length; i++) {
            if(fn.call(this, arr[i], val)) {
                col.insert(arr[i]);
            }
        }
        return col;
    }
        
    public function toString():String {
        var str:String = new String();
        for(var i:uint = 0; i < arr.length - 1; i++) {
            str += arr[i] + ", ";
        }
        str += arr[arr.length - 1];
        return str;
    }
}

class EventListener {
    public function EventListener() {
    }
    
    public function itemInsertedHandler(obj:Object):void {
        trace("itemInsertedHandler: " + obj);
    }
    
    public function itemRemovedHandler(obj:Object):void {
        trace("itemRemovedHandler: " + obj);        
    }
}




 

评论添加到页面后给我发送电子邮件 | 评论报告

当前页: http://livedocs.adobe.com/flash/9.0_cn/ActionScriptLangRefV3/Function.html