| 包 | 顶级 |
| 类 | public final class Function |
| 继承 | Function Object |
| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
类的方法与 Function 对象略有不同。与普通函数对象不同,方法和与其关联的类对象紧密关联。因此,方法或属性具有在同一类的所有实例中共享的定义。可以从实例提取方法并将其处理为“绑定”方法(保留与原始实例的链接)。对于绑定方法,this 关键字指向实现该方法的原始对象。对于函数,this 在调用函数时指向关联对象。
| 方法 | 由以下参数定义 | ||
|---|---|---|---|
指定要在 ActionScript 调用的任何函数内使用的 thisObject 的值。 | Function | ||
调用 Function 对象表示的函数。 | Function | ||
![]() |
表示对象是否已经定义了指定的属性。 | Object | |
![]() |
表示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
表示指定的属性是否存在、是否可枚举。 | Object | |
![]() |
设置循环操作动态属性的可用性。 | Object | |
![]() |
返回此对象的字符串表示形式,其格式设置遵守区域设置特定的约定。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() |
返回指定对象的原始值。 | Object | |
apply | () | 方法 |
AS3 function apply(thisArg:*, argArray:*):*| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
指定要在 ActionScript 调用的任何函数内使用的 thisObject 的值。此方法还指定要传递给任何被调用函数的参数。由于 apply() 是 Function 类的方法,所以它也是 ActionScript 中每个 Function 对象的方法。
与 Function.call() (它将参数指定为用逗号分隔的列表)不同,该方法将参数指定为一个 Array 对象。如果在脚本实际执行前,无法知道要传递的参数的数量,那么这种方法通常很有用。
返回被调用函数指定为返回值的值。
参数
thisArg:* (default = NaN) — 要应用该函数的对象。
| |
argArray:* (default = NaN) — 其元素作为参数传递给函数的数组。
|
* — 调用函数指定的任何值。
|
相关 API 元素
call | () | 方法 |
AS3 function call(thisArg:*, ... args):*| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
调用 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)
返回被调用函数指定为返回值的值。
参数
thisArg:* (default = NaN) — 指定函数体内 thisObject 值的对象。
| |
... args — 要传递给该函数的参数。可以指定 0 个或多个参数。
|
* |
相关 API 元素
FunctionExample、SimpleCollection、EventBroadcaster 和 EventListener 类显示 ActionScript 中函数的各种用法。这是由以下步骤完成的:
FunctionExample的构造函数创建一个名为simpleColl的局部变量,该变量用一个整数数组填充,其范围从1到8。- 使用
trace()打印simpleColl对象。 - 将 EventListener 对象
listener添加到simpleColl中。 - 调用
insert()和remove()函数时,侦听器响应其事件。 - 创建第二个 SimpleCollection 对象,名为
greaterThanFourColl。 - 赋予
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);
}
}
Tue Jun 12 2018, 11:04 AM Z
隐藏继承的公共属性
显示继承的公共属性