What [object global] is exactly, when “this” is accessed inside a nested or inline function?

为君一笑 提交于 2019-12-25 03:05:42

问题


Here is the code:

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

    }

}

The output is :

[object global]

[object SomeDocumentClass]

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


回答1:


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

Anonymous functions

  • 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

Defined functions

  • 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

So as example:

this in an Anonymous function

[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 in a Defined function

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


来源:https://stackoverflow.com/questions/33200045/what-object-global-is-exactly-when-this-is-accessed-inside-a-nested-or-inli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!