确切地说,什么是“ [全局对象]”?在嵌套或内联函数内部访问? [英] What [object global] is exactly, when "this" is accessed inside a nested or inline function?

查看:113
本文介绍了确切地说,什么是“ [全局对象]”?在嵌套或内联函数内部访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

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]

[对象SomeDocumentClass ]

[object SomeDocumentClass]

我也检查过, [对象全局] 甚至不是该函数本身。

I also checked, [object global] is not even the function itself actually. Then what is it?

推荐答案

匿名函数(也称为函数表达式)的作用域是 global 。定义的函数(也称为函数语句)为 local 范围。

Anonymous functions (also called function expressions) are global scoped vs. defined functions (also called function statements) local scoped.

匿名函数


  • 不能用作方法

  • 仅存在于定义范围内

  • 只能在定义的范围内调用

  • 可以在代码中的任意位置调用

  • 可以重新分配新的值或已删除

  • Cannot be used as a method
  • Exists only in the scope in which it is defined
  • Can only be called in the scope in which it is defined
  • Can be called at any point in the code
  • Can be reassigned a new value or deleted

定义的功能


  • 可以用作对象的方法

  • 在其定义的对象中存在

  • 可以在以下位置调用代码中的任何点

  • 无法删除或更改

  • Can be used as a method of an object
  • Exists within the object it is defined in
  • Can be called at any point in the code
  • Cannot be deleted or changed

例如:

在匿名函数中

[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剪切/粘贴完成示例:

ActionScript cut/paste complete example:

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();
    }
}

这篇关于确切地说,什么是“ [全局对象]”?在嵌套或内联函数内部访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆