以下是代码:
package {
import flash.events.*;
import flash.display.*;
public class SomeDocumentClass extends MovieClip {
public var abc: MovieClip;
public var test_var = 12345;
public function SomeDocumentClass() {
//abc is an instance name of a movieclip placed on stage
abc.addEventListener(MouseEvent.CLICK,
function () {
trace(this);
});
abc.addEventListener(MouseEvent.CLICK, abc_CLICK)
}
function abc_CLICK(e: Event): void {
trace(this);
}
}
}
输出结果为:
[object global]
[object SomeDocumentClass]
我也检查过,[object global]
实际上甚至不是函数本身。那是什么?
答案 0 :(得分:2)
匿名函数(也称为函数表达式)是全局作用域与已定义函数(也称为函数语句) local 作用域。
匿名函数
定义的功能
例如:
匿名函数中的 this
[trace] [object global]
[trace] global
[trace] <type name="global" base="Object" isDynamic="true" isFinal="true" isStatic="false">
[trace] <extendsClass type="Object"/>
[trace] <constant name="FLASH10_FLAGS" type="uint" uri="avmplus"/>
~~~~~~~~~~~~
[trace] </method>
[trace] </type>
定义函数中的 this
[trace] [object Main]
[trace] Main
[trace] <type name="Main" base="flash.display::Sprite" isDynamic="false" isFinal="false" isStatic="false">
[trace] <extendsClass type="flash.display::Sprite"/>
~~~~~~~~~~
[trace] </metadata>
[trace] </type>
ActionScript剪切/粘贴完整示例:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import avmplus.getQualifiedClassName;
import flash.utils.describeType;
public class Main extends Sprite {
var button:CustomSimpleButton;
public function Main() {
button = new CustomSimpleButton();
button.addEventListener(MouseEvent.CLICK,
function () {
trace(this);
trace(getQualifiedClassName(this));
trace(describeType(this));
});
button.addEventListener(MouseEvent.CLICK, onClickButton);
addChild(button);
}
function onClickButton(event:MouseEvent):void {
trace(this);
trace(getQualifiedClassName(this));
trace(describeType(this));
}
}
}
import flash.display.Shape;
import flash.display.SimpleButton;
class CustomSimpleButton extends SimpleButton {
private var upColor:uint = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0x00CCFF;
private var size:uint = 80;
public function CustomSimpleButton() {
downState = new ButtonDisplayState(downColor, size);
overState = new ButtonDisplayState(overColor, size);
upState = new ButtonDisplayState(upColor, size);
hitTestState = new ButtonDisplayState(upColor, size * 2);
hitTestState.x = -(size / 4);
hitTestState.y = hitTestState.x;
useHandCursor = true;
}
}
class ButtonDisplayState extends Shape {
private var bgColor:uint;
private var size:uint;
public function ButtonDisplayState(bgColor:uint, size:uint) {
this.bgColor = bgColor;
this.size = size;
draw();
}
private function draw():void {
graphics.beginFill(bgColor);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
}
}